Reputation: 1797
I keep getting and IllegalStateException
W/System.err: java.lang.IllegalStateException: Fragment RunScenarioFragment{8b6deff} (3523a253-d2c8-49f3-95ea-33bbbc0c308a)} not attached to a context.
on this line:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.green_check);
I have looked at other posts but am somewhat new to android studio and was having trouble adapting their solutions to my problem. Any help would be appreciated.
UPDATE BASED ON COMMENTS
This does not happen every time. Only after I switch screens/fragments (bottom navigation) and click on the item which initiates this process. It takes place in an anonymous routine in the onCreate method of the fragment.
Upvotes: 0
Views: 239
Reputation: 1530
The getResources() method internally returns requireActivity().getResources() when called from a fragment.
requireActivity() throws IllegalStateException if not currently associated with an activity or if associated only with a context.
You mentioned that your code in called from onCreate method of the fragment. This method can be called while the fragment's activity is still in the process of being created.
Move the code snippet from onCreate method to onCreateView method if it has to do with instantiating the user interface; or you can move it to onActivityCreated.
Upvotes: 1