Why does my application crash when I quickly switch fragments?

My applications sometimes crash when I change a fragment using navigationDrawer. Fatal error doesn't help much, how can I solve this problem? thanks

FATAL EXCEPTION: main Process: acr.acr_app, PID: 29425 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.view.ViewConfiguration.get(ViewConfiguration.java:359) at android.view.View.<init>(View.java:3656) at android.view.View.<init>(View.java:3751) at android.view.ViewGroup.<init>(ViewGroup.java:492) at android.widget.LinearLayout.<init>(LinearLayout.java:200) at android.widget.LinearLayout.<init>(LinearLayout.java:196) at android.widget.LinearLayout.<init>(LinearLayout.java:192) at android.widget.LinearLayout.<init>(LinearLayout.java:188) at android.widget.TableRow.<init>(TableRow.java:61) at acr.acr_app.MyFragment3$2.onChildAdded(MyFragment3.java:170) at com.google.android.gms.internal.zzaer.zza(Unknown Source) at com.google.android.gms.internal.zzagp.zzSu(Unknown Source) at com.google.android.gms.internal.zzags$1.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) 

Fragment3 line170: onStart () listener

  tableRow = new TableRow(getContext()); tableRow.setLayoutParams(new TableLayout.LayoutParams( TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1.0f)); 
+5
source share
2 answers

The reason for this is that the accident fragment is still executing code, while it is already separate from your activity.

In your case, the Fragment already switches to another fragment when it reaches getContext() . Since getContext() not looking for activity (that the fragment is no longer attached to) this will throw a NullPointer exception.

Try the following:

  if(isAdded()){ tableRow = new TableRow(getContext()); tableRow.setLayoutParams(new TableLayout.LayoutParams( TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT, 1.0f)); } 
+4
source

What is the operation mode of your fragmentManager fragmentManager ? Is it :replace or showhide ? It will work if you switch quickly because your fragment did not complete initialization when it joins the FragmentActivity . Try something like this:

0
source

All Articles