FragmentTransaction在人像模式引发NullPointerException异常 [英] FragmentTransaction in Portrait Mode Throws NullPointerException

查看:162
本文介绍了FragmentTransaction在人像模式引发NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先第一件事情,我使用兼容性库,这里是我得到的错误:

First things first, I'm using the compatibility library and here's the error I'm getting:

11-22 19:57:09.111: ERROR/AndroidRuntime(28867): FATAL EXCEPTION: main
        java.lang.NullPointerException
        at android.support.v4.app.BackStackRecord.bumpBackStackNesting(BackStackRecord.java:518)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:563)
        at 

    android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1379)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:419)
            at android.os.Handler.handleCallback(Handler.java:587)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:132)
            at android.app.ActivityThread.main(ActivityThread.java:4123)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:491)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
            at dalvik.system.NativeStart.main(Native Method)

只有当应用程序在纵向和我试图用FragmentTransaction隐藏其他两个片段,并显示一个新的会出现此错误。这code,当用户presses在应用程序中的一个按钮得到执行。

This error only occurs when the application is in PORTRAIT orientation and I'm attempting to use a FragmentTransaction to hide two other fragments and show a new one. This code gets executed when a user presses a button in the app.

这里的code抛出(只要提交被调用,并得到执行上述错误被抛出)。

Here's the code that throws (as soon as the commit is called and gets executed the error above is being thrown).

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.hide(fragmentA);
transaction.hide(fragmentB);
transaction.replace(R.id.fragment_container, new PurchaseFragment());
transaction.commit();

任何想法,为什么这可能发生?

Any ideas why this might be happening?

推荐答案

想通了这是什么。我使用Roboguice注入一些组件,并且由于上面没有显示出一些逻辑,片段空。如果任何片段是空的 transaction.hide(片段)之后就会​​出现上述异常。

Figured out what it was. I'm using Roboguice to inject some components and due to some logic not shown above, the fragments were null. If any fragment is null the transaction.hide(fragment) then the above exception will occur.

我固定逻辑错误后,片段不再为空。在这一点上的应用程序工作正常。

After I fixed the logic error, the fragments were no longer null. At that point the app worked as expected.

这篇关于FragmentTransaction在人像模式引发NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆