【发布时间】:2020-05-19 18:18:33
【问题描述】:
我正在使用 Room 数据库。在视频指南上做了。 在视频中,活动布局包含:一个数据库对象列表,一个允许您添加和更改记录的添加按钮。 我需要将此代码分成几部分。 我已经可以做一个单独的列表,单独更改记录,但是我不能做数据。
第一个代码显示有效的原始代码,第二个显示我的。
第一个代码
@Override
public void onItemClick(Note note) {
Intent intent = new Intent(MainActivity.this, AddEditNoteActivity.class);
intent.putExtra(AddEditNoteActivity.EXTRA_ID, note.getId());
intent.putExtra(AddEditNoteActivity.EXTRA_TITLE, note.getTitle());
intent.putExtra(AddEditNoteActivity.EXTRA_DESCRIPTION, note.getDescription());
intent.putExtra(AddEditNoteActivity.EXTRA_PRIORITY, note.getPriority());
startActivityForResult(intent, EDIT_NOTE_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ADD_NOTE_REQUEST && resultCode == RESULT_OK) {
String title = data.getStringExtra(AddEditNoteActivity.EXTRA_TITLE);
String description = data.getStringExtra(AddEditNoteActivity.EXTRA_DESCRIPTION);
int priority = data.getIntExtra(AddEditNoteActivity.EXTRA_PRIORITY, 1);
Note note = new Note(title, description, priority);
noteViewModel.insert(note);
Toast.makeText(this, "Note saved", Toast.LENGTH_SHORT).show();
} else if (requestCode == EDIT_NOTE_REQUEST && resultCode == RESULT_OK) {
int id = data.getIntExtra(AddEditNoteActivity.EXTRA_ID, -1);
if (id == -1) {
Toast.makeText(this, "Note can't be updated", Toast.LENGTH_SHORT).show();
return;
}
String title = data.getStringExtra(AddEditNoteActivity.EXTRA_TITLE);
String description = data.getStringExtra(AddEditNoteActivity.EXTRA_DESCRIPTION);
int priority = data.getIntExtra(AddEditNoteActivity.EXTRA_PRIORITY, 1);
Note note = new Note(title, description, priority);
note.setId(id);
noteViewModel.update(note);
Toast.makeText(this, "Note updated", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Note not saved", Toast.LENGTH_SHORT).show();
}
}
第二个代码
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.Toast;
public class Add_item extends AppCompatActivity implements View.OnClickListener {
public static NoteViewModel noteViewModel1;
private EditText editTextTitle;
private EditText editTextDescription;
private NumberPicker numberPickerPriority;
Button save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
save = findViewById(R.id.Save);
save.setOnClickListener(this);
editTextTitle = findViewById(R.id.edit_text_title);
editTextDescription = findViewById(R.id.edit_text_description);
numberPickerPriority = findViewById(R.id.number_picker_priority);
numberPickerPriority.setMinValue(1);
numberPickerPriority.setMaxValue(10);
}
private void saveNotes() {
String title = editTextTitle.getText().toString();
String description = editTextDescription.getText().toString();
int priority = numberPickerPriority.getValue();
if (title.trim().isEmpty() || description.trim().isEmpty()) {
Toast.makeText(this, "Please insert a title and description", Toast.LENGTH_SHORT).show();
return;
}
Note note = new Note(title, description, priority);
noteViewModel1.insert(note);
Toast.makeText(this, "Note saved", Toast.LENGTH_SHORT).show();
finish();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.Save:
saveNotes();
}
}
}
错误
05-19 21:01:30.440 26571-26571/com.example.recview E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.recview, PID: 26571
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.recview.NoteViewModel.insert(com.example.recview.Note)' on a null object reference
at com.example.recview.Add_item.saveNotes(Add_item.java:41)
at com.example.recview.Add_item.onClick(Add_item.java:51)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5480)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
【问题讨论】:
-
找不到“noteViewModel1”的初始化。
标签: android android-room