After closing the program and if you start the program immediately after a second after closing, the program opens, but when I want to update the data in one of the fragments

ContentResolver resolver = getActivity().getContentResolver(); 

getActivity() returns a null.

 01-16 14:45:48.168 6361-6361/com.example.eldos.callreport W/System.err﹕ java.lang.NullPointerException 01-16 14:45:48.168 6361-6361/com.example.eldos.callreport W/System.err﹕ at com.example.eldos.callreport.fragments.Fragment2.computRepForRecycle(Fragment2.java:239) 

What can be wrong, something is not deleted during closing or is not created during opening?

    1 answer 1

    We read documentation and google

    Caution: If you need a context, you can call getActivity (). However, it is only when the fragment is attached to the activity. If you want to get rid of the lifecycle, it will receive return null.

    • I did this: Activity mActivity; @Override public void onAttach (Activity activity) {super. (Activity); mActivity = activity; } public Activity getmActivity () {if (getActivity () == null) return mActivity; else return getActivity (); } Works. - J Mas
    • @metalurgus Thank you - J Mas
    • everything is ok, if you call getmActivity () before onAttach (), it will return null - Vladyslav Matviienko
    • @metalurgus as I understood it, it should be called in onCreatedActivity, I did, but the error came out, so when I need to call it, I read the documentation and maybe I didn’t understand something, because of bad English. - J Mas
    • 2
      The link to the activation can not be stored thoughtlessly. When onStop () is called, the links need to be reset, otherwise you will have memory leaks. Perform your actions at the right moment. If getActivity () returns null, then you have problems with logic, and you should think about it. - andreich