Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why my app crash when I switch fragments fast?

My apps crash sometimes when I change the fragment with the navigationDrawer. The fatal error isn't helping much, How can I solve this problem? Thx

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));
like image 491
Xaloju Avatar asked Aug 19 '16 09:08

Xaloju


People also ask

Why are my Apps crashing all of a sudden?

This usually occurs when your Wi-Fi or cellular data is slow or unstable, and apps tend to malfunction. Another reason for Android apps crashing problem is the lack of storage space in your device. This occurs when you overload your device's internal memory with heavy apps as well.

Why do my Apps keep crashing on Android How do you fix it?

Clear the app cache Cached files can get corrupted, though, and might cause the app to keep crashing or freezing. To clear the app cache, go to Settings > Storage > Other apps > (app name) and tap on Clear cache. You can also find this option by going to Settings > Apps > See all apps > (app name) > Storage.

Why do my game Apps keep crashing?

You may have downloaded the app improperly, and all you need to do is to reinstall the app to fix the crashing problem: Go to Settings > “Apps” or “Application manager” > Choose the app that crashes > Tap the “Uninstall” option to make it. Then you can go to Google Play Store to reinstall the app after a few minutes.


1 Answers

The reason for this crash is that the fragment is still executing code while it is already detached from your Activity.

In your case the Fragment is already switched to another Fragment when it reaches the getContext(). Because getContext() is looking for the Activity (which the fragment is no longer attached to) it will cause 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));
 }
like image 52
MrJM Avatar answered Oct 20 '22 05:10

MrJM