1

我注意到在我的 Android 应用程序中,在离开片段后,仍然调用onCreate()and方法。onDestroy()这会导致我的代码抛出异常,因为该onDestroy()方法引用了ViewModel在片段导航离开时不可用的资源(特别是 my )。

public class InfoFragment extends Fragment {

    private InfoViewModel viewModel;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate");

        /* ... other initialization code ... */
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy");
        viewModel.performOperation(); // NullPointerException occurs after fragment is navigated away from, and screen is rotated
    }

(请注意,这些不是片段和视图模型的真实名称)

为什么我onCreate()onDestroy()片段在离开它后仍然被调用?这可能是内存泄漏吗?

编辑:这是我的崩溃日志。

java.lang.RuntimeException: Unable to destroy activity {com.example.app/com.example.app.ui.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.lifecycle.MutableLiveData com.example.app.ui.InfoViewModel.performOperation()' on a null object reference
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4551)
    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4569)
    at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4853)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4786)
    at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:113)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:71)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1858)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6820)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.lifecycle.MutableLiveData com.example.app.ui.InfoViewModel.performOperation()' on a null object reference
    at com.example.app.ui.InfoFragment.onDestroy(InfoFragment.java:202)
    at androidx.fragment.app.Fragment.performDestroy(Fragment.java:2927)
    at androidx.fragment.app.FragmentStateManager.destroy(FragmentStateManager.java:492)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1296)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
    at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1504)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
    at androidx.fragment.app.FragmentManager.dispatchDestroy(FragmentManager.java:2609)
    at androidx.fragment.app.Fragment.performDestroy(Fragment.java:2922)
    at androidx.fragment.app.FragmentStateManager.destroy(FragmentStateManager.java:492)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1296)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
    at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
    at androidx.fragment.app.FragmentManager.dispatchDestroy(FragmentManager.java:2609)
    at androidx.fragment.app.FragmentController.dispatchDestroy(FragmentController.java:330)
    at androidx.fragment.app.FragmentActivity.onDestroy(FragmentActivity.java:365)
    at androidx.appcompat.app.AppCompatActivity.onDestroy(AppCompatActivity.java:242)
    at android.app.Activity.performDestroy(Activity.java:7524)
    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1307)
    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4536)
    ... 13 more

当片段仍然是当前片段时不会发生此异常,但仅当用户通过底部导航栏从它导航到其他片段时才会发生此异常。

编辑:澄清一下,我不是在问为什么会发生崩溃(甚至可以忽略崩溃),我想知道为什么在导航离开片段后仍然调用onCreateand方法。onDestroy该片段应该在导航离开后被销毁,但它似乎仍在运行,因为我仍然从 logcat 接收 onCreate 和 onDestroy 的日志。为什么会这样?

谢谢。

4

1 回答 1

0

你的日志显示

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'androidx.lifecycle.MutableLiveData com.example.app.ui.InfoViewModel.performOperation()'

并且您的视图模型尚未声明,请尝试在 onCreate 方法中声明它

于 2020-12-05T02:40:12.033 回答