I've making an android app with bottom navigation. There's a main activity and three items on the navigation menu, three of each corresponds to a fragment. In the 'modeFragment', I've add a "add button" to switch to a new fragment, which I name it 'NewNoteFragment', in this fragment I can enter words and press the 'save' button to turn back to 'modeFragment'.
I have used viewmodel and room database so far, and I implement only one function --> 'insert' function to insert my data into the database.
While I run the app, the demo app crashes when I press the save button. I wonder if it is the viewmodel's problem, but I can't find any solution online.
I am a newbie in developing Android app and I don't have a clue to how to fix the problem.
This is the code where the problem is.
noteViewModel.insert(note);
This is modeFragment
public class modeFragment extends Fragment {
private String TAG = this.getClass().getSimpleName();
private NoteViewModel noteViewModel;
private TextView mTextTitle;
//private static final int NEW_NOTE_ACTIVITY_REQUEST_CODE = 1;
public modeFragment() {
// Requires empty public constructor
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mode, container, false);
mTextTitle = (TextView) view.findViewById(R.id.test);
Bundle bundle = getArguments();
Log.d("MainActivity", "Yeah man");
if(bundle != null) {String note_description = bundle.getString("etNewNote");
//final String note_id = UUID.randomUUID().toString();
System.out.println(note_description);
Note note = new Note(note_description, NOTE_ADDED);
System.out.println(note.getNote());
Log.d("MainActivity", "Don't make me do this");
noteViewModel.insert(note);
//TextView firstText = (TextView) view.findViewById(R.id.etNewNote);
}
FloatingActionButton fab = view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NewNoteFragment newNotefragment = new NewNoteFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Log.d("MainActivity", "I love you");
fragmentTransaction.add(R.id.frag_mode, newNotefragment);
Log.d("MainActivity", "I hate you");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
NoteViewModel noteViewModel = new ViewModelProvider(this).get(NoteViewModel.class);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
mTextTitle.setText("Fire in the home(x) hole(o)!");
// Setup any handles to view objects here
// EditText etFoo = (EditText) view.findViewById(R.id.etFoo);
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
};}
This is 'NewNoteFragment'
public class NewNoteFragment extends Fragment implements View.OnClickListener{
private static final int REQUEST_CODE = 1;
public static final String NOTE_ADDED = "new_note";
private EditText etNewNote;
public NewNoteFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_new_note, container, false);
Log.d("MainActivity", "Dancing is great");
etNewNote = view.findViewById(R.id.etNewNote);
Button add=(Button) view.findViewById(R.id.Add);
add.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
if(TextUtils.isEmpty(etNewNote.getText())){
return;
}else {
String first = etNewNote.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("etNewNote", first);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
modeFragment ModeFragment = new modeFragment();
ModeFragment.setArguments(bundle);
fragmentTransaction.hide(this);
fragmentTransaction.add(R.id.fragment_container, ModeFragment);
fragmentTransaction.commit();
}
}
}
This is NoteViewModel class
public class NoteViewModel extends AndroidViewModel{
private String TAG = this.getClass().getSimpleName();
private NoteDao noteDao;
private NoteRoomDatabase noteDB;
public NoteViewModel(Application application) {
super(application);
noteDB = NoteRoomDatabase.getDatabase(application);
noteDao = noteDB.noteDao();
}
public void insert(Note note) {
new InsertAsyncTask(noteDao).execute(note);
}
@Override
protected void onCleared() {
super.onCleared();
Log.i(TAG, "ViewModel Destroyed");
}
private class InsertAsyncTask extends AsyncTask<Note, Void, Void> {
NoteDao mNoteDao;
public InsertAsyncTask(NoteDao mNoteDao) {
this.mNoteDao = mNoteDao;
}
@Override
protected Void doInBackground(Note... notes) {
mNoteDao.insert(notes[0]);
return null;
}
}
}
This is the "Note" class
@Entity(tableName = "notes")
public class Note {
@NonNull
public String getDescription() {
return description;
}
@NonNull
public String getNote() {
return this.mNote;
}
@PrimaryKey
@NonNull
private String description;
@NonNull
@ColumnInfo(name = "note")
private String mNote;
public Note(String description, String note){
this.description = description;
this.mNote = note;
}
}
This is the logcat Debug session and the error
Process: com.demo.iot, PID: 16605
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.demo.iot.NoteViewModel.insert(com.demo.iot.Note)' on a null object reference
at com.demo.iot.modeFragment.onCreateView(modeFragment.java:57)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:310)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1185)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1354)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1432)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1495)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2167)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1990)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1945)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1847)
at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Thank you for you help in advance, I would really appreciate it.
Instantiate the viewmodel before call them
Try onCreateView
like follow
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mode, container, false);
mTextTitle = (TextView) view.findViewById(R.id.test);
//Instantiate the viewmodel before call them
noteViewModel = new ViewModelProvider(this).get(NoteViewModel.class);
Bundle bundle = getArguments();
Log.d("MainActivity", "Yeah man");
if(bundle != null) {String note_description = bundle.getString("etNewNote");
//final String note_id = UUID.randomUUID().toString();
System.out.println(note_description);
Note note = new Note(note_description, NOTE_ADDED);
System.out.println(note.getNote());
Log.d("MainActivity", "Don't make me do this");
noteViewModel.insert(note);
//TextView firstText = (TextView) view.findViewById(R.id.etNewNote);
}
FloatingActionButton fab = view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NewNoteFragment newNotefragment = new NewNoteFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Log.d("MainActivity", "I love you");
fragmentTransaction.add(R.id.frag_mode, newNotefragment);
Log.d("MainActivity", "I hate you");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return view;
}