与FragmentManager.popBackStack相关的NullPointerException.怎么解决? [英] NullPointerException related to FragmentManager.popBackStack. How to resolve?
问题描述
我有一个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)
产生此错误的代码是:
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来响应此按钮单击.仅添加上面的代码似乎可以防止应用崩溃,但不能以期望的方式更改应用的用户界面.
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屋!