Error Description:
Make a network request in the fragment, jump to other pages before the request ends, and then jump back from other pages. The compiler reports the following error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mio.hygrothermograph, PID: 32449
java.lang.IllegalStateException: Fragment HomeFragment{86a99ac} (d6728a71-7df5-4f7b-ae10-e3e449d3b451) not attached to an activity.
at androidx.fragment.app.Fragment.requireActivity(Fragment.java:928)
at com.mio.hygrothermograph.ui.HomeFragment$getHomeInfoByRxJava$1.onNext(HomeFragment.kt:91)
at com.mio.hygrothermograph.ui.HomeFragment$getHomeInfoByRxJava$1.onNext(HomeFragment.kt:70)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:201)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:255)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:119)
at android.os.Handler.handleCallback(Handler.java:900)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8668)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)
reason
Execute a time-consuming task in the Fragment, and rebuild the Activity before the task is completed. Getactivity() will be null, and null pointer exceptions will be caused in all places where parameters need to be obtained through the activity. For example, the getResource() method.
Solution:
The isAdded() judgment is performed where the parameters need to be obtained through the Activity to judge whether the current Fragment is added to the activity. Only when the Fragment is added to the activity can getResource be obtained.
Read More:
- [Solved] Could not identify launch activity: Default Activity not found Error while Launching activity
- [Solved] Could not identify launch activity: Default Activity not found Error while Launching activity
- Could not identify launch activity: Default Activity not foundError while Launching activity
- [Solved] Android DataBinding Error: java.lang.ArrayIndexOutOfBoundsException: length=2; index=2 fragment Changes to Fragm
- Remember an android app startup error Error running: Default Activity not found
- How to Solve Error: Android java.lang.IllegalStateException: Could not execute method of the activity
- How to Solve Error: You need to use a Theme.AppCompat theme (or descendant) with this activity.
- [Solved] flutter Project Error: ScrollController attached to multiple scroll views, Failed assertion: line 109 pos 12
- [Solved] Manifest merger failed: android:exported needs to be explicitly specified for element <activity#com
- Error while Launching activity [How to Solve]
- [Solved] Message from debugger: debug-server is x86_64 binary running in translation, attached failed.
- JZVideo Error: Attempt to invoke virtual method ‘android.view.Window android.app.Activity.getWindow()’ on a null object reference
- [Solved] Android Studio Error: error while launching activity
- Kettle reported an error and did not have permission to write files to HDFS
- Uniapp: TypeError: Cannot read property ‘apply‘ of undefined (H5 does not report an error, but the real machine runs with an error)
- Introduce wangeditor5 to run an error: You may need an appropriate loader to handle this file type (es6 compatibility issue)
- laravel Error mews/captcha is locked to version 3.2.4 and an update of this package was not requested.
- [Solved] A needed class was not found. This could be due to an error in your runpath. Missing class: scala/co
- [Solved] ERROR_POLICY: attempt to perform an operation not allowed by the security policy `PDF‘ @ error/const
- IDEA reports an error Could not reserve enough space for xxxxKB object heap processing