Fragment.setNextTransition(int,int)抛出NullPointerException

时间:2017-03-06 07:51:15

标签: android android-fragments fragment-backstack

我为我的应用程序使用支持片段(在onCreate()中使用setRetainInstance(true))。有时候app会抛出NullPointerException:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.livemaster.zhurnal/ru.livemaster.zhurnal.activity.main.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setNextTransition(int, int)' on a null object reference
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
       at android.app.ActivityThread.access$1100(ActivityThread.java:221)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:158)
       at android.app.ActivityThread.main(ActivityThread.java:7224)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setNextTransition(int, int)' on a null object reference
       at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:716)
       at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2303)
       at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2132)
       at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
       at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
       at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
       at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
       at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181)
       at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1265)
       at android.app.Activity.performStart(Activity.java:6915)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3216)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
       at android.app.ActivityThread.access$1100(ActivityThread.java:221)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:158)
       at android.app.ActivityThread.main(ActivityThread.java:7224)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

在BackStackRecord中的这个地方发生异常:

 void executeOps() {
        final int numOps = mOps.size();
        for (int opNum = 0; opNum < numOps; opNum++) {
            final Op op = mOps.get(opNum);
            final Fragment f = op.fragment;
            f.setNextTransition(mTransition, mTransitionStyle); //Exception occurs in this place
            switch (op.cmd) {
                ...
            }
            if (!mAllowOptimization && op.cmd != OP_ADD) {
                mManager.moveFragmentToExpectedState(f);
            }
        }
        if (!mAllowOptimization) {
            // Added fragments are added at the end to comply with prior behavior.
            mManager.moveToState(mManager.mCurState, true);
        }
    }

我不知道它何时发生,因为该错误出现在 crashlytics 中。

也许我做错了什么?我开始使用这段代码从堆栈中删除所有片段:

public static void popWholeBackStack(FragmentManager fragmentManager) {
    if(fragmentManager.getBackStackEntryCount() > 0) {
        BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(0);
        if(backStackEntry != null) {
            fragmentManager.popBackStack(backStackEntry.getId(), 1);
        }
    }

}

比我执行替换事务:

protected static <T extends RichFragment> void replaceFragment(FragmentManager fragmentManager, T fragment, Bundle bundle) {
    fragment.setArguments(bundle);
    popWholeBackStack(fragmentManager);
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    setAnimation(transaction);
    transaction.replace(fragment.getContainerId(), fragment, getFragmentTag(fragment));
    transaction.commit();
}

它能否以某种方式导致崩溃?

0 个答案:

没有答案