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; } }
contextnull - YuriySPb ♦notesDBHelper = new notesDBHelper(context);to transfer toonResume? - Lateral Glebcontextpopulated and why should it be stored when there is agetContext()/getActivity()method? Attach a full stack-length and not a snippet. - temq