每当我从MainActivity中单击FloatingActionButton时,我的应用程序就会崩溃。我的logcat中出现以下致命错误:
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.androidmks.roomvmliverecyclerviewexample / com.androidmks.roomvmliverecyclerviewexample.AddNoteActivity}: java.lang.NullPointerException:尝试调用虚拟方法 'android.view.View android.widget.EditText.findViewById(int)'在 空对象引用
我看到它来自一个EditText空对象引用,但是我不知道在哪里找到它。
package com.androidmks.roomvmliverecyclerviewexample;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.List;
public class MainActivity extends AppCompatActivity implements LifecycleOwner {
public static final int ADD_NOTE_REQUEST=1;
private NoteViewModel noteViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton buttonAddNote = findViewById(R.id.button_add_note);
buttonAddNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AddNoteActivity.class);
startActivityForResult(intent,ADD_NOTE_REQUEST);
}
});
RecyclerView recyclerView = findViewById(R.id.recycler_viewer);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
final NoteAdapter adapter = new NoteAdapter();
recyclerView.setAdapter(adapter);
noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() {
@Override
public void onChanged(List<Note> notes) {
adapter.setNotes(notes);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode== ADD_NOTE_REQUEST && resultCode == RESULT_OK){
String title = data.getStringExtra(AddNoteActivity.EXTRA_TITLE);
String description = data.getStringExtra(AddNoteActivity.EXTRA_DESCRIPTION);
int priority = data.getIntExtra(AddNoteActivity.EXTRA_PRIORITY,1);
Note note= new Note(title,description,priority);
noteViewModel.insert(note);
Toast.makeText(this, "Note Saved", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Note not Saved", Toast.LENGTH_SHORT).show();
}
}
}
AddNoteActivity.java
package com.androidmks.roomvmliverecyclerviewexample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.Toast;
public class AddNoteActivity extends AppCompatActivity {
public static final String EXTRA_TITLE =
"com.androidmks.roomvmliverecyclerviewexample.EXTRA_TITLE";
public static final String EXTRA_DESCRIPTION =
"com.androidmks.roomvmliverecyclerviewexample.EXTRA_DESCRIPTION";
public static final String EXTRA_PRIORITY =
"com.androidmks.roomvmliverecyclerviewexample.EXTRA_PRIORITY";
private EditText editTextTitle;
private EditText editTextDescription;
private NumberPicker numberPickerPriority;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_note);
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);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
setTitle("Add Note");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.add_note_menu, menu);
return true;
}
private void saveNote() {
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;
}
Intent data = new Intent();
data.putExtra(EXTRA_TITLE, title);
data.putExtra(EXTRA_DESCRIPTION, description);
data.putExtra(EXTRA_PRIORITY, priority);
setResult(RESULT_OK, data);
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save_note:
saveNote();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_add_note.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".AddNoteActivity">
<EditText
android:id="@+id/edit_text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"
android:inputType="text"/>
<EditText
android:id="@+id/edit_text_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Description"
android:inputType="textMultiLine"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Priority:"
android:layout_marginTop="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
<NumberPicker
android:id="@+id/number_picker_priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></NumberPicker>
</LinearLayout>
LOGCAT
'2019-05-26 17:46:35.894 22330-22330 / com.androidmks.roomvmliverecyclerviewexample E / AndroidRuntime:致命异常:main 流程:com.androidmks.roomvmliverecyclerviewexample,PID:22330 java.lang.RuntimeException:无法启动活动ComponentInfo {com.androidmks.roomvmliverecyclerviewexample / com.androidmks.roomvmliverecyclerviewexample.AddNoteActivity}: java.lang.NullPointerException:尝试调用虚拟方法 'android.view.View android.widget.EditText.findViewById(int)'在 空对象引用 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6669) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 原因:java.lang.NullPointerException:尝试调用虚拟方法'android.view.View 空对象引用上的android.widget.EditText.findViewById(int)' 在com.androidmks.roomvmliverecyclerviewexample.AddNoteActivity.onCreate(AddNoteActivity.java:30) 在android.app.Activity.performCreate(Activity.java:7136) 在android.app.Activity.performCreate(Activity.java:7127) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:193) 在android.app.ActivityThread.main(ActivityThread.java:6669) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)'