尝试从Button按下调用showDialog()方法时获取NullPointerException

时间:2011-10-19 20:47:29

标签: android dialog nullpointerexception

每当在特定视图中按下按钮时,我都会尝试显示自定义对话框。但是,每当我尝试切换到包含按钮的视图时,我都会收到强制关闭错误。 LogCat在我用来设置监听器的方法上给我一个NullPointerException,但是,我不确定是否存在错误。我认为它可能是Dialog代码,但我已将其写入Android Developers网站的规范。

以下是使用按钮侦听器的方法:

//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

P.S。我之前已经发布过这个问题,但实际的问题正在解决,所以我删除了并重新发布。谢谢你的帮助!

1 个答案:

答案 0 :(得分:2)

您如何获得对这些对象的引用?这样的事情?

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

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

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

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