java.lang.IllegalStateException: Fragment not attached to Activity

前端 未结 12 1072
名媛妹妹
名媛妹妹 2020-11-28 02:28

I am rarely getting this error while making an API call.

java.lang.IllegalStateException: Fragment  not attached to Activity

I tried puttin

相关标签:
12条回答
  • 2020-11-28 02:38

    I Found Very Simple Solution isAdded() method which is one of the fragment method to identify that this current fragment is attached to its Activity or not.

    we can use this like everywhere in fragment class like:

    if(isAdded())
    {
    
    // using this method, we can do whatever we want which will prevent   **java.lang.IllegalStateException: Fragment not attached to Activity** exception.
    
    }
    
    0 讨论(0)
  • 2020-11-28 02:41

    Fragment lifecycle is very complex and full of bugs, try to add:

    Activity activity = getActivity(); 
    if (isAdded() && activity != null) {
    ...
    }
    
    0 讨论(0)
  • 2020-11-28 02:42

    Exception: java.lang.IllegalStateException: Fragment

    DeadlineListFragment{ad2ef970} not attached to Activity

    Category: Lifecycle

    Description: When doing time-consuming operation in background thread(e.g, AsyncTask), a new Fragment has been created in the meantime, and was detached to the Activity before the background thread finished. The code in UI thread(e.g.,onPostExecute) calls upon a detached Fragment, throwing such exception.

    Fix solution:

    1. Cancel the background thread when pausing or stopping the Fragment

    2. Use isAdded() to check whether the fragment is attached and then to getResources() from activity.

    0 讨论(0)
  • 2020-11-28 02:42

    Sometimes this exception is caused by a bug in the support library implementation. Recently I had to downgrade from 26.1.0 to 25.4.0 to get rid of it.

    0 讨论(0)
  • 2020-11-28 02:47

    I adopted the following approach for handling this issue. Created a new class which act as a wrapper for activity methods like this

    public class ContextWrapper {
        public static String getString(Activity activity, int resourceId, String defaultValue) {
            if (activity != null) {
                return activity.getString(resourceId);
            } else {
                return defaultValue;
            }
        }
    
        //similar methods like getDrawable(), getResources() etc
    
    }
    

    Now wherever I need to access resources from fragments or activities, instead of directly calling the method, I use this class. In case the activity context is not null it returns the value of the asset and in case the context is null, it passes a default value (which is also specified by the caller of the function).

    Important This is not a solution, this is an effective way where you can handle this crash gracefully. You would want to add some logs in cases where you are getting activity instance as null and try to fix that, if possible.

    0 讨论(0)
  • 2020-11-28 02:47

    this happen when the fragment does not have a context ,thus the getActivity()method return null. check if you use the context before you get it,or if the Activity is not exist anymore . use context in fragment.onCreate and after api response usually case this problem

    0 讨论(0)
提交回复
热议问题