我在我的活动中定义了一个按钮并按如下方式初始化:
et_Btn =(按钮)findViewById(R.id.editor_btn_add);
资源是:
<Button
android:id="@+id/editor_btn_add"
android:layout_gravity="right"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/editor_btn_add" />
并为其onclick事件创建一个处理程序,如下所示:
et_Btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Log.v("click message","hey");
//Open Contact Intent to pick contact ID
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,1001);
}
});
但是当我点击按钮时,它会给我一个错误:
02-27 19:07:05.840: E/AndroidRuntime(892): Uncaught handler: thread main exiting due to uncaught exception
02-27 19:07:05.859: E/AndroidRuntime(892): java.lang.RuntimeException: Unable to pause activity {com.iBirthDayNotofication/com.iBirthDayNotofication.BirthdayEditor}: java.lang.NullPointerException
02-27 19:07:05.859: E/AndroidRuntime(892): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3162)
02-27 19:07:05.859: E/AndroidRuntime(892): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3119)
02-27 19:07:05.859: E/AndroidRuntime(892): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3102)
02-27 19:07:05.859: E/AndroidRuntime(892): at android.app.ActivityThread.access$2400(ActivityThread.java:119)
02-27 19:07:05.859: E/AndroidRuntime(892): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1870)
02-27 19:07:05.859: E/AndroidRuntime(892): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 19:07:05.859: E/AndroidRuntime(892): at android.os.Looper.loop(Looper.java:123)
02-27 19:07:05.859: E/AndroidRuntime(892): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-27 19:07:05.859: E/AndroidRuntime(892): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 19:07:05.859: E/AndroidRuntime(892): at java.lang.reflect.Method.invoke(Method.java:521)
02-27 19:07:05.859: E/AndroidRuntime(892): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-27 19:07:05.859: E/AndroidRuntime(892): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-27 19:07:05.859: E/AndroidRuntime(892): at dalvik.system.NativeStart.main(Native Method)
02-27 19:07:05.859: E/AndroidRuntime(892): Caused by: java.lang.NullPointerException
02-27 19:07:05.859: E/AndroidRuntime(892): at com.iBirthDayNotofication.BirthdayEditor.saveState(BirthdayEditor.java:180)
02-27 19:07:05.859: E/AndroidRuntime(892): at com.iBirthDayNotofication.BirthdayEditor.onSaveInstanceState(BirthdayEditor.java:151)
02-27 19:07:05.859: E/AndroidRuntime(892): at android.app.Activity.performSaveInstanceState(Activity.java:1022)
02-27 19:07:05.859: E/AndroidRuntime(892): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180)
02-27 19:07:05.859: E/AndroidRuntime(892): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3144)
02-27 19:07:05.859: E/AndroidRuntime(892): ... 12 more
有什么问题?