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!
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.