尝试通过按下按钮调用 showDialog() 方法时出现 NullPointerException

发布于 2024-12-11 01:10:49 字数 4089 浏览 0 评论 0原文

我试图在特定视图中按下按钮时显示自定义对话框。但是,每当我尝试切换到包含按钮的视图时,都会出现强制关闭错误。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栩栩如生 2024-12-18 01:10:49

您如何获得对这些对象的引用?像这样的东西?

mTimeButton = (Button)findViewById(R.id.timeBtn);

如果是这样,你什么时候这样做?如果您调用 findViewById() 时视图不在屏幕上,它将返回 null 给您。

发布你的 onCreate 方法,这样会更容易弄清楚发生了什么。

编辑:还要向引发异常的行添加注释。因为我们无法分辨哪一个是 52。

How are you getting your reference to these objects? something like this?

mTimeButton = (Button)findViewById(R.id.timeBtn);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文