首页 > 解决方案 > Kotlin 协程重复调用 Log.e 语句并在 ViewmodelScope 中崩溃应用程序

问题描述

我正在尝试制作一个应用程序,我可以在其中访问设备上的文件夹并收集数据,并且该应用程序在大多数设备上都可以正常运行,但是在某些设备上,当我尝试通过在所有函数中记录语句以查看执行进行调试时,它无法正常工作代码然后我发现在协程中一切似乎都很好,但是突然重复触发了一个 Log 语句,然后应用程序抛出了一些错误并崩溃了应用程序

try {
                viewModelScope.launch(Dispatchers.Default) {
                    Log.e("Debug", "Coroutine started")
                    val root = File(StickerContentProvider.FILES_LOCATION)
                    val state = Environment.getExternalStorageState()
                    if (Environment.MEDIA_MOUNTED == state || Environment.MEDIA_MOUNTED_READ_ONLY == state) {
                        var count: Int = 0
                        root.walkBottomUp().forEach {
    
                            if (it.name.endsWith(".webp"))
                            {
                                val webp : WebPImage = WebPImage.createFromByteArray(it.readBytes())
                                if(animated && webp.frameCount > 1 && webp.height == 512 &&  webp.width == 512 )
                                {
                                    fileList.add(it)
                                    Log.e("Debug", it.name + " added")
                                }
                                else
                                if(!animated && webp.frameCount <= 1)
                                {
                                    fileList.add(it)
                                    Log.e("Debug", it.name + " added")
    
                                }
    
                            }
                        }
                        fileList.sortWith(Comparator.comparingLong(File::lastModified).reversed())
                        Log.e("Debug", "adding sorted items ")
                        while(count != 100) {
                            list.add(Items( BitmapFactory.decodeFile(fileList[count].path),fileList[count].name, Uri.fromFile(fileList[count]),fileList[count].absoluteFile.totalSpace.toString()))
                            count++
                        }
                        Images.postValue(list)
                    }
                }
            }
            catch (e:Exception)
            {
                Log.e("Exception", e.message!! )
            } 

此代码运行良好,但在某些设备中,其崩溃日志如下

E/Debug: Animated button pressed
W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@c865242
E/Debug: getStickers called from viewmodel init block
E/Debug: Coroutine started
E/Debug: 08_AreYouOK.webp added
E/Debug: 09_StayingHomeMug.webp added
E/Debug: 10_WorkingFromBed.webp added
E/Debug: 11_StayCalm.webp added
E/Debug: 12_Gymnastics.webp added
E/Debug: 13_DoubleChecking.webp added
E/Debug: 14_CatOnTheLaptop.webp added
E/Debug: 15_WorkingFromHomeF.webp added
E/Debug: 16_WorkingFromHomeM.webp added
E/Debug: 17_WashingHands.webp added
E/Debug: 18_DontTouchYourFace.webp added
E/Debug: 19_SocialDistancing.webp added
E/Debug: 20_SuperheroNurse.webp added
E/Debug: 21_YouAreMyHero.webp added
E/Debug: 01_SendingLove.webp added
E/Debug: 02_WellDoThisTogether.webp added
E/Debug: 03_Heart.webp added
E/Debug: 04_AirHighFive.webp added
E/Debug: 05_GroupVideoCalling.webp added
W/System: A resource failed to call close. 
W/System: A resource failed to call close. 
E/Debug: 06_StayConnected.webp added
W/System: A resource failed to call close. 
E/Debug: 07_OK.webp added
W/System: A resource failed to call close. 
    A resource failed to call close. 
E/Debug: adding sorted items 
I/chatty: uid=10120(com.MJ.stickerapp) DefaultDispatch identical 8 lines
E/Debug: adding sorted items 
D/OnePlusJankManager:  Chor uploadMDM JANK_TYPE_ONCE mViewTitle = com.MJ.stickerapp/com.MJ.stickerapp.activities.StickerPackListActivity--- jank level = 1
E/Debug: adding sorted items 
I/chatty: uid=10120(com.MJ.stickerapp) DefaultDispatch identical 10 lines
E/Debug: adding sorted items 
E/System: Unable to open zip file: /data/app/com.MJ.stickerapp-ZKUUnVCklAHkXTW8cw_kEA==/base.apk
E/System: java.io.FileNotFoundException: File doesn't exist: /data/app/com.MJ.stickerapp-ZKUUnVCklAHkXTW8cw_kEA==/base.apk
        at java.util.zip.ZipFile.<init>(ZipFile.java:215)
        at java.util.zip.ZipFile.<init>(ZipFile.java:152)
        at java.util.jar.JarFile.<init>(JarFile.java:160)
        at java.util.jar.JarFile.<init>(JarFile.java:97)
        at libcore.io.ClassPathURLStreamHandler.<init>(ClassPathURLStreamHandler.java:47)
        at dalvik.system.DexPathList$Element.maybeInit(DexPathList.java:716)
        at dalvik.system.DexPathList$Element.findResource(DexPathList.java:743)
        at dalvik.system.DexPathList.findResources(DexPathList.java:533)
        at dalvik.system.BaseDexClassLoader.findResources(BaseDexClassLoader.java:270)
        at java.lang.ClassLoader.getResources(ClassLoader.java:839)
        at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:349)
        at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:402)
        at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:488)
        at kotlin.sequences.SequencesKt___SequencesKt.toCollection(_Sequences.kt:786)
        at kotlin.sequences.SequencesKt___SequencesKt.toMutableList(_Sequences.kt:816)
        at kotlin.sequences.SequencesKt___SequencesKt.toList(_Sequences.kt:807)
        at kotlinx.coroutines.CoroutineExceptionHandlerImplKt.<clinit>(CoroutineExceptionHandlerImpl.kt:23)
        at kotlinx.coroutines.CoroutineExceptionHandlerImplKt.handleCoroutineExceptionImpl(CoroutineExceptionHandlerImpl.kt:27)
        at kotlinx.coroutines.CoroutineExceptionHandlerKt.handleCoroutineException(CoroutineExceptionHandler.kt:33)
        at kotlinx.coroutines.StandaloneCoroutine.handleJobException(Builders.common.kt:192)
        at kotlinx.coroutines.JobSupport.finalizeFinishingState(JobSupport.kt:229)
        at kotlinx.coroutines.JobSupport.tryMakeCompletingSlowPath(JobSupport.kt:906)
        at kotlinx.coroutines.JobSupport.tryMakeCompleting(JobSupport.kt:863)
        at kotlinx.coroutines.JobSupport.makeCompletingOnce$kotlinx_coroutines_core(JobSupport.kt:828)
        at kotlinx.coroutines.AbstractCoroutine.resumeWith(AbstractCoroutine.kt:100)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)

标签: javaandroidandroid-studiokotlinkotlin-coroutines

解决方案


推荐阅读