尝试通过按下按钮调用 showDialog() 方法时出现 NullPointerException
我试图在特定视图中按下按钮时显示自定义对话框。但是,每当我尝试切换到包含按钮的视图时,都会出现强制关闭错误。 LogCat 在我用来设置侦听器的方法上给了我一个 NullPointerException,但是,我不确定为什么存在错误。我认为这可能是对话框代码,但我已将其写入 Android 开发人员网站的规范。
这是带有按钮侦听器的方法:
//Registers the listeners for the various buttons in the edit view
private void registerButtonListenersAndSetDefaultText() {
mTimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_PICKER_DIALOG);
}
});
updateTimeButtonText();
mDayButton.setOnClickListener(new View.OnClickListener() { //here is where
//the error is
//being thrown.
public void onClick(View v) {
showDialog(DAY_CHECKBOX_DIALOG);
}
});
}
这是 onCreateDialog() 方法:
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = new Dialog(this);
switch(id) {
case TIME_PICKER_DIALOG:
showTimePicker();
case DAY_CHECKBOX_DIALOG:
showDayPicker();
}
return onCreateDialog(id);
}
这是 showDayPicker() 方法(我认为可能导致问题的方法):
private Dialog showDayPicker() {
Dialog dialog = new Dialog(ScheduleEditActivity.this);
dialog.setContentView(R.layout.day_picker_dialog);
dialog.setTitle("Choose A Day");
return dialog;
}
这是 LogCat:
10-19 20:23:57.191: ERROR/AndroidRuntime(293): FATAL EXCEPTION: main
10-19 20:23:57.191: ERROR/AndroidRuntime(293): java.lang.RuntimeException: Unable to start activity ComponentInfo{vt.nhw.android.easyringertoggle/vt.nhw.android.easyringertoggle.ScheduleEditActivity}: java.lang.NullPointerException
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.os.Looper.loop(Looper.java:123)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at java.lang.reflect.Method.invokeNative(Native Method)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at java.lang.reflect.Method.invoke(Method.java:521)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at dalvik.system.NativeStart.main(Native Method)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): Caused by: java.lang.NullPointerException
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at vt.nhw.android.easyringertoggle.ScheduleEditActivity.registerButtonListenersAndSetDefaultText(ScheduleEditActivity.java:52)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at vt.nhw.android.easyringertoggle.ScheduleEditActivity.onCreate(ScheduleEditActivity.java:39)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): ... 11 more
PS 我之前已经发布过这个问题,但实际的问题主体很复杂,所以我删除并重新发布。感谢您的帮助!
I am trying to show a custom Dialog whenever a button is pressed in a specific view. However, I get force close error whenever I try to switch to the view containing the button. LogCat is giving me a NullPointerException on the method that I am using to set the listener, however, I am not sure whey there is an error. I think it may be the Dialog code, but I have written it to Android Developers site's specs.
Here is the method with the button listener:
//Registers the listeners for the various buttons in the edit view
private void registerButtonListenersAndSetDefaultText() {
mTimeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(TIME_PICKER_DIALOG);
}
});
updateTimeButtonText();
mDayButton.setOnClickListener(new View.OnClickListener() { //here is where
//the error is
//being thrown.
public void onClick(View v) {
showDialog(DAY_CHECKBOX_DIALOG);
}
});
}
Here is the onCreateDialog() method:
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = new Dialog(this);
switch(id) {
case TIME_PICKER_DIALOG:
showTimePicker();
case DAY_CHECKBOX_DIALOG:
showDayPicker();
}
return onCreateDialog(id);
}
Here is the showDayPicker() method (the one I think may be causing the issue):
private Dialog showDayPicker() {
Dialog dialog = new Dialog(ScheduleEditActivity.this);
dialog.setContentView(R.layout.day_picker_dialog);
dialog.setTitle("Choose A Day");
return dialog;
}
And here is the LogCat:
10-19 20:23:57.191: ERROR/AndroidRuntime(293): FATAL EXCEPTION: main
10-19 20:23:57.191: ERROR/AndroidRuntime(293): java.lang.RuntimeException: Unable to start activity ComponentInfo{vt.nhw.android.easyringertoggle/vt.nhw.android.easyringertoggle.ScheduleEditActivity}: java.lang.NullPointerException
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.os.Looper.loop(Looper.java:123)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at java.lang.reflect.Method.invokeNative(Native Method)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at java.lang.reflect.Method.invoke(Method.java:521)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at dalvik.system.NativeStart.main(Native Method)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): Caused by: java.lang.NullPointerException
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at vt.nhw.android.easyringertoggle.ScheduleEditActivity.registerButtonListenersAndSetDefaultText(ScheduleEditActivity.java:52)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at vt.nhw.android.easyringertoggle.ScheduleEditActivity.onCreate(ScheduleEditActivity.java:39)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-19 20:23:57.191: ERROR/AndroidRuntime(293): ... 11 more
P.S. I have posted this question before, but the actual question body got convoluted so I deleted and reposted. Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您如何获得对这些对象的引用?像这样的东西?
如果是这样,你什么时候这样做?如果您调用 findViewById() 时视图不在屏幕上,它将返回 null 给您。
发布你的 onCreate 方法,这样会更容易弄清楚发生了什么。
编辑:还要向引发异常的行添加注释。因为我们无法分辨哪一个是 52。
How are you getting your reference to these objects? something like this?
If so when are you doing that? If the view is not on the screen at the time you make the findViewById() call it is going to return null to you.
Post your onCreate method and it'll be easier to figure out what is going on.
Edit: Also add a comment to the line that the exception is being thrown on. Since we can't tell which one is 52.