java.lang.IllegalStateException: Fragment not attached to Activity

前端 未结 12 1163
名媛妹妹
名媛妹妹 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 03:00

    This issue occurs whenever you call a context which is unavailable or null when you call it. This can be a situation when you are calling main activity thread's context on a background thread or background thread's context on main activity thread.

    For instance , I updated my shared preference string like following.

    editor.putString("penname",penNameEditeText.getText().toString());
    editor.commit();
    finish();
    

    And called finish() right after it. Now what it does is that as commit runs on main thread and stops any other Async commits if coming until it finishes. So its context is alive until the write is completed. Hence previous context is live , causing the error to occur.

    So make sure to have your code rechecked if there is some code having this context issue.

提交回复
热议问题