我只是想setOnClickListener圖像按鈕上的方法,它會導致空指針異常錯誤。如下圖所示NullPointerException:與OnClickListener啓動方法
的logcat:
02-25 10:49:43.051 13936-13936/com.example.l33902.contactmanagment1512 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.l33902.contactmanagment1512, PID: 13936
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.l33902.contactmanagment1512/com.example.l33902.contactmanagment.EditActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object referenceat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3155)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263)
at android.app.ActivityThread.access$1000(ActivityThread.java:197)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6897)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.l33902.contactmanagment.EditActivity.onCreate(EditActivity.java:158)
at android.app.Activity.performCreate(Activity.java:6550)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3108)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3263)
at android.app.ActivityThread.access$1000(ActivityThread.java:197)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6897)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
以下是我的相關代碼:
ImageButton saveButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_activity);
saveButton = (ImageButton) findViewById(R.id.edit_activity_save);
displayNameEditText = (EditText) findViewById(R.id.edit_activity_text_cal_name);
displayNameEditTextLyout = (TextInputLayout) findViewById(R.id.edit_activity_text_cal_name_layout);
saveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
save();
}
});
}
private void save() {
if (displayNameEditText.getText().length() == 0) {
displayNameEditTextLyout.setError(getString(R.string.edit_activity_error_empty_name));
} else {
displayNameEditTextLyout.setError(null);
if (edit)
updateCalendar();
else
addCalendar(EditActivity.this);
}
}
private void updateCalendar() {
CalendarController.updateCalendar(mCalendarId, displayNameEditText.getText()
.toString(), colorPicker.getColor(), getContentResolver());
finish();
}
在我的佈局XML:
<ImageButton
android:id="@+id/edit_activity_save"
android:layout_width="64dp"
android:layout_height="64dp"
android:contentDescription="@string/edit_activity_save"
android:src="@drawable/ic_save_white_24dp"/>
我想不出有什麼錯誤約我似乎已經宣佈並將一切指向正確的方法。
你真的在你的xml中有id爲'edit_activity_save'的按鈕嗎? –