我正在使用recyclerView
来显示sdCard中的图像/视频,例如图库应用程序。最近,我迁移到Kotlin并尝试使用协程进行后台工作。
但是启动协程时出现随机崩溃,我无法理解Firebase Crashlytics显示的堆栈跟踪,它显示发生崩溃的类和行号,但没有引起崩溃的原因。
以下是我发生故障的refresh()
方法。
private fun refreshGallery() {
gridView.adapter = null
if (actionMode != null) actionMode!!.finish()
myAdapter = Adapter(galleryType!!, this@BaseFragment) //This is where I am getting a crash on some devices...
ref.isRefreshing = true
launch {
delay(1000)
val statusList = getMediaFiles()
myAdapter!!.addAll(statusList)
myAdapter!!.setEmptyView(empty)
gridView.adapter = myAdapter
myAdapter!!.toogleEmptyView()
if (ref.isRefreshing) ref.isRefreshing = false
}
}
在onCreateView()
和swipeRefresh.setOnRefreshListener{}
中调用此方法
更多信息,我在Fragment中扩展了CoroutineScope
,所以没有Global.launch{}
我还尝试对函数使用suspend
修饰符,将withContext()
更改为async{}.await
,但仍然崩溃。
这是Crashlytics的日志-
Caused by e.b
at com.example.fragments.BaseFragment.refreshGallery + 352(BaseFragment.java:352)
at com.example.fragments.BaseFragment.onCreateView + 233(BaseFragment.java:233)
at androidx.fragment.app.Fragment.onCreateAnimator(Fragment.java:5)
at androidx.fragment.app.Fragment.performCreateView + 2595(Fragment.java:2595)
at androidx.fragment.app.FragmentManagerImpl.moveToState + 880(FragmentManagerImpl.java:880)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState + 1237(FragmentManagerImpl.java:1237)
at androidx.fragment.app.FragmentManagerImpl.moveToState + 1302(FragmentManagerImpl.java:1302)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange + 2655(FragmentManagerImpl.java:2655)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated + 2609(FragmentManagerImpl.java:2609)
at androidx.fragment.app.FragmentController.dispatchActivityCreated + 246(FragmentController.java:246)
at androidx.fragment.app.FragmentActivity.onStart + 542(FragmentActivity.java:542)
at androidx.appcompat.app.AppCompatActivity.onStart + 201(AppCompatActivity.java:201)
at com.example.MainActivity.onStart + 189(MainActivity.java:189)
at android.app.Instrumentation.callActivityOnStart + 1249(Instrumentation.java:1249)
at android.app.Activity.performStart + 6873(Activity.java:6873)
at android.app.ActivityThread.performLaunchActivity + 2687(ActivityThread.java:2687)
at android.app.ActivityThread.handleLaunchActivity + 2785(ActivityThread.java:2785)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage + 1532(ActivityThread.java:1532)
at android.os.Handler.dispatchMessage + 102(Handler.java:102)
at android.os.Looper.loop + 163(Looper.java:163)
at android.app.ActivityThread.main + 6342(ActivityThread.java:6342)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run + 880(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main + 770(ZygoteInit.java:770)
MainActivity的onStart()
-
override fun onStart() {
super.onStart()
try {
NotificationManagerCompat.from(this@MainActivity).cancelAll()
} catch (ignore: Exception) {}
}
答案 0 :(得分:0)
第一步应该是获取有关构建的混淆mapping.txt
并找到e.b
映射到的内容。
但是,从上下文来看,我的猜测是它映射到kotlin.KotlinNullPointerException
,如果被断言为非null的变量(通过!!
)实际上变为{{ 1}}。
由于这与协程无关,并且与该变量(null
)相关的代码未包含在您的问题中,因此无法进一步调试。