android - NullPointerException related to FragmentManager.popBackStack. How to resolve? -


i have android app have activity , stack of fragments.

using crashlytics, have received single instance of following exception:

fatal exception: java.lang.nullpointerexception: attempt invoke virtual method 'void android.app.fragmentmanager.popbackstack(java.lang.string, int)' on null object reference        @ com.company.app.fragment$7$2.onclick(fragment.java:397)        @ android.view.view.performclick(view.java:5197)        @ android.view.view$performclick.run(view.java:20926)        @ android.os.handler.handlecallback(handler.java:739)        @ android.os.handler.dispatchmessage(handler.java:95)        @ android.os.looper.loop(looper.java:145)        @ android.app.activitythread.main(activitythread.java:5942)        @ java.lang.reflect.method.invoke(method.java)        @ java.lang.reflect.method.invoke(method.java:372)        @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1399)        @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1194) 

the code producing error is:

okbutton.setonclicklistener(new view.onclicklistener() {     @override     public void onclick(view v) {         dialog.dismiss();         // next line produces exception         getfragmentmanager().popbackstack(null, fragmentmanager.pop_back_stack_inclusive);         new handler().postdelayed(new runnable() {             @override             public void run() {                 // other code...         }, 250);     } }); 

based on testing have done app, user exception occurred, , fact has occurred once, thinking exception occurs when strange consolidation of circumstances occurs. (basically, can't reproduce exception on end.) assuming has user backgrounding app , resuming @ later time, , getfragmentmanager() call returns null.

so, know can "fix" code following (found applied fix github repository):

fragmentmanager fm = getfragmentmanager(); if (fm != null) fm.popbackstack(); 

while realize code above "fix" problem in avoid npe, (thus stopping app crashing), doesn't "fix" problem allowing app function desired. fragment in question #3 in stack of fragments, this:

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

the desired behavior app respond button click popping fragment #1 being visible. merely adding in code above seems keep app crashing, not change app's ui in desired manner.

what doing wrong such when app resumes, it's "fragment state" out of whack?

i have never faced issue getfragmentmanager() being null assuming popback part of statement causing issue. here 1 possible solution prevent crash , handling exceptions. if happened once, may have been rare instance.

okbutton.setonclicklistener(new view.onclicklistener() {     @override     public void onclick(view v) {         if (getsupportfragmentmanager() != null) {             // assuming getfragmentmanager() not issue, rather popbackstack issue             try {                 getfragmentmanager().popbackstack(null, fragmentmanager.pop_back_stack_inclusive);             } catch (exception e) {                 // recreate new instance of first fragment here.             }         } else {             /*              getfragmentmanager() == null               have never faced issue when getfragmentmanager() == null, restart activity if case             */          }         new handler().postdelayed(new runnable() {             @override             public void run() {              }         }, 250);     } }); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -