与 FragmentManager.popBackStack 相关的 NullPointerException.如何解决? [英] NullPointerException related to FragmentManager.popBackStack. How to resolve?

查看:20
本文介绍了与 FragmentManager.popBackStack 相关的 NullPointerException.如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Android 应用,其中有一个活动和一堆片段.

I have an Android app where I have an activity and a stack of fragments.

使用 Crashlytics,我收到了以下 Exception 的单个实例:

Using Crashlytics, I have received a single instance of the following Exception:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.FragmentManager.popBackStack(java.lang.String, int)' on a null object reference
       at com.company.app.Fragment$7$2.onClick(Fragment.java:397)
       at android.view.View.performClick(View.java:5197)
       at android.view.View$PerformClick.run(View.java:20926)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:5942)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

产生这个错误的代码是:

The code that is producing this error is:

okButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        dialog.dismiss();
        // The next line produces the Exception
        getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // Other code...
        }, 250);
    }
});

基于我对这个应用程序所做的测试、发生此异常的用户以及仅发生一次的事实,我认为这个 Exception 发生在一些奇怪的合并时的情况发生.(基本上,我无法在我的最后重现这个 Exception.)我假设这与用户后台我的应用程序,然后在稍后恢复它有关,然后是 getFragmentManager() 调用返回一个 null.

Based on the testing I have done with this app, the user for which this Exception occurred, and the fact that is has only occurred once, I am thinking that this Exception occurs when some strange consolidation of circumstances occurs. (Basically, I can't reproduce this Exception on my end.) I am assuming this has to do with the user backgrounding my app and then resuming it at a later time, and then the getFragmentManager() call returns a null.

所以,我知道我可以使用如下代码修复"它(发现这是对 GitHub 存储库的应用修复):

So, I know that I can "fix" this with code like the following (found this as an applied fix to a GitHub repository):

FragmentManager fm = getFragmentManager();
if (fm != null) fm.popBackStack();

虽然我意识到上面的代码将修复"问题,因为它将避免 NPE,(从而阻止应用程序崩溃),但它并没有真正修复"问题通过允许我的应用程序按需要运行.有问题的 Fragment 是一堆片段中的 #3,如下所示:

While I realize that the code above will "fix" the problem in that it will avoid the NPE, (thus stopping the app from crashing), it doesn't really "fix" the problem by allowing my app to function as desired. The Fragment in question is #3 in a stack of fragments, like this:

#1 --> #2 --> #3

应用程序所需的行为是通过弹出回Fragment #1 可见来响应此按钮点击.仅仅添加上面的代码似乎可以防止应用程序崩溃,但不会以所需的方式更改应用程序的 UI.

The desired behavior for the app is to respond to this button click by popping back to Fragment #1 being visible. Merely adding in the code above seems like it would keep the app from crashing, but not change the app's UI in the desired manner.

我做错了什么,以至于当我的应用恢复时,它的片段状态"不正常?

What am I doing wrong such that when my app resumes, it's "fragment state" is out of whack?

推荐答案

我从来没有遇到过 getFragmentManager() 为 null 的问题,所以我假设它是导致问题的语句的 popBack 部分.这是防止崩溃和处理异常的一种可能的解决方案.如果它只发生过一次,那可能是一个罕见的例子.

I have never faced an issue with the getFragmentManager() being null so I am assuming it is the popBack part of the statement that is causing the issue. Here is one possible solution to prevent a crash and handling the exceptions. If it only happened once, it may have been a rare instance.

okButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (getSupportFragmentManager() != null) {
            // Assuming the getFragmentManager() is not the Issue, rather the popBackStack is the issue
            try {
                getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            } catch (Exception e) {
                // Recreate a new instance of your first fragment here.
            }
        } else {
            /*
             getFragmentManager() == null 
             I have never faced an issue when getFragmentManager() == null, but I would restart the activity if that is the case
            */

        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

            }
        }, 250);
    }
});

这篇关于与 FragmentManager.popBackStack 相关的 NullPointerException.如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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