我的应用程序遇到了很多崩溃,我无法复制这些崩溃中的任何一个,甚至无法使用Crashlytics生成的崩溃报告进行追溯。

这是一种情况:

class ColumnDisplayFragment : BaseFragment() {

    private lateinit var viewModelType: ColumnDetailViewModelType

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setHasOptionsMenu(true)
        showBackButton(true)
        setActionBarTitle(viewModelType.title)
        recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            setHasFixedSize(true)
            adapter = ColumnDetailAdapter(viewModelType)
        }
    }

    companion object {
      fun newInstance(viewModelType: ColumnDetailViewModelType) = ColumnDetailFragment().apply {
          this.viewModelType = viewModelType
      }
    }
}

这就是我创建片段的方式:
ColumnDetailFragment.newInstance(viewModelType)

我的ViewModelType:
interface ColumnDetailViewModelType: Serializable {
    val columns: List<Column>
    val title: String
    val clickableColumns: List<Column>
    val chartButtons: List<ButtonType>
    val menu: Int
    val itemSelected: BehaviorSubject<Column>
    val buttonModel: DetailColumnButtonCell.DetailColumnButtonModel
    val showFragmentObservable: Observable<ShowDetailFragment>
    val showPopUps: Observable<ShowPopDetailUp>

    fun bindCloseButton(clicks: Observable<Unit>)
    fun onMenuItemSelected(item: Int)
}

但是,在某处, viewModelType 丢失了,并导致崩溃。

这是Crashlytics中生成的崩溃报告:
Fatal Exception: kotlin.UninitializedPropertyAccessException: lateinit property viewModelType has not been initialized
       at com.app.android.traderpro.etx.fragments.columnDetailFragments.ColumnDetailFragment.onViewCreated(ColumnDetailFragment.java:51)
       at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:892)
       at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
       at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
       at androidx.fragment.app.BackStackRecord.executePopOps(BackStackRecord.java:500)
       at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2076)
       at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
       at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
       at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:310)
       at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:253)
       at androidx.fragment.app.FragmentManagerImpl.handleOnBackPressed(FragmentManagerImpl.java:233)
       at androidx.fragment.app.FragmentManagerImpl$1.handleOnBackPressed(FragmentManagerImpl.java:108)
       at androidx.activity.OnBackPressedDispatcher.onBackPressed(OnBackPressedDispatcher.java:189)
       at androidx.activity.ComponentActivity.onBackPressed(ComponentActivity.java:286)
       at com.app.android.traderpro.etx.activities.homeActivity.HomeActivity.onBackPressed(HomeActivity.java:202)
       at android.app.Activity.onKeyUp(Activity.java:3756)
       at android.view.KeyEvent.dispatch(KeyEvent.java:2949)
       at android.app.Activity.dispatchKeyEvent(Activity.java:4092)
       at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:115)
       at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
       at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:133)
       at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:558)
       at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
       at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2814)
       at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
       at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:454)
       at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5742)
       at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5610)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5058)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5111)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5077)
       at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5234)
       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5085)
       at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5291)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5058)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5111)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5077)
       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5085)
       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:5058)
       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5111)
       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5077)
       at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5267)
       at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:5437)
       at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:3072)
       at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2615)
       at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2606)
       at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:3049)
       at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:143)
       at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
       at android.os.MessageQueue.next(MessageQueue.java:363)
       at android.os.Looper.loop(Looper.java:173)
       at android.app.ActivityThread.main(ActivityThread.java:8147)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)


但是,当我尝试复制它时,一切似乎都可以正常运行,并且该应用程序没有崩溃。任何帮助,将不胜感激。

谢谢。

最佳答案



欢迎使用Process Death导致的生产崩溃,您的应用程序将被系统终止以回收资源,然后返回到应用程序,瞧瞧,未保存在setArgumentsintent.putExtraonSaveInstanceState中的内容(也未保存到磁盘上)要么),完全丢失了:)

Singleton object becomes null after app is resumed中所述:



因此,考虑到这一点,问题在于您设置了一个实例字段,而不是通过viewModelType传递了setArguments

companion object {
  fun newInstance(viewModelType: ColumnDetailViewModelType) = ColumnDetailFragment().apply {
      this.arguments = Bundle().also { bundle ->
          putSerializable("viewModelType", viewModelType)
      }
  }
}

在另一边:
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    viewModelType = requireArguments().getSerializable("viewModelType")
}

07-24 15:00