First, I took the data (say, notes entered from the keyboard) from the database in the onCreateView fragment where these notes are displayed, but when I worked up the function of adding new notes in another activity, I found that when I return to the previous activity, new notes do not appear until restart the application. I concluded that the onCreateView method onCreateView not executed when returning to the previous activity, and it is necessary to write the code for receiving data in onResume :

 @Override public void onResume() { super.onResume(); getNoteData(); } 

As you know , onResume() is always executed: both after and after onCreate , therefore, data can be retrieved to avoid duplication of code only in onResume . Now the code looks like:

 @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(LAYOUT, container, false); notesDBHelper = new notesDBHelper(context); return view; } @Override public void onResume() { super.onResume(); getNoteData(); } 

The error below occurs when restarting main activity from Android Studio ; I have not found any other conditions for the occurrence of this error. Probably, after the end of development, this error will not occur, but now we need to understand why it occurs.

 E/AndroidRuntime: FATAL EXCEPTION: main Process: jp.co.yd.infowajg.ydjks, PID: 11599 java.lang.RuntimeException: Unable to resume activity {ru.example/ru.example.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference ... at jp.co.yd.infowajg.ydjks.fragment.InboxTabFragment.onResume(InboxTabFragment.java:50) 

The last line (from what I copied and pasted) just refers to

 getNoteData() 

Update Added all the code of the Fragment-class.

 public class NotesTabFragment extends AbstractTabFragment{ private static final int LAYOUT = R.layout.fragment_notes; NotesDBHelper notesDBHelper; public static NotesTabFragment getInstance(Context context){ Bundle args = new Bundle(); NotesTabFragment fragment = new NotesTabFragment(); fragment.setArguments(args); fragment.setContext(context); return fragment; } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(LAYOUT, container, false); notesDBHelper = new NotesDBHelper(context); return view; } @Override public void onResume() { super.onResume(); getNotesItems(); } private void getNotesItems(){ RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycleView); recyclerView.setLayoutManager(new LinearLayoutManager(context)); recyclerView.setAdapter(new InboxItemsAdapter(notesDBHelper.getAllNotes())); } public void setContext(Context context) { this.context = context; } } 
  • You have context null - YuriySPb
  • Then maybe notesDBHelper = new notesDBHelper(context); to transfer to onResume ? - Lateral Gleb
  • Better show where and how the context is initialized. Or replace it with getActivity () - JuriySPb
  • How is the context populated and why should it be stored when there is a getContext()/getActivity() method? Attach a full stack-length and not a snippet. - temq
  • temq, no problem, updated question field. - Bokov Gleb

1 answer 1

It makes little sense to store the context in a separate variable.

In addition, you use it completely incorrectly in your case — to set it to a fragment when creating an instance of it is absolutely wrong.

You need to getActivity() context in the fragment using getActivity()

So just delete your context variable and replace it with a call to the getActivity() method

  • In onCreateView do as you said - no problem, but what about getInstance ? By the way, this method is made on this sample. - Bokov Gleb
  • @GurebuBokofu, maybe I don’t know something, but I’ve never seen anything like that. I am inclined to believe that the author of the video did not think a bit. Context is the connection between an application component and the system. As long as the fragment is not added to the activation, it will not invoke life-cycle methods and it does not need the context, because it exists in the "airless" space. I repeat - maybe I do not know something, but personally, in my many years of experience, the transfer of context in getInstance() is game and gloom. Yes, and this method itself is called newInstance() because it is not a singleton - Yuriy SPb
  • Thank you for the explanation. And how then is it better to initialize the fragment? Can you give any reference to an example written according to the rules of good tone? - Bokov Gleb
  • @GurebuBokofu, simply delete the context variable and do not pass it into the fragment when you create it. - Yuriy SPb
  • one
    @GurebuBokofu the context of the fragment appears after the onAttach onAttach() RC method, so there is no need to transfer the context from the outside like that this will create errors after rebuilding the fragment. Sometimes it is more useful to read the official documentation instead of watching videos with dubious content, or at least look at it sometimes - developer.android.com/guide/components/fragments.html?hl=en- temq