我目前正在试图引发一个DatePickerDialog,每当一个EditText被点击。但是,每次单击EditText时,应用程序都会崩溃。这里是相关的代码和LogCat。Android - onCreateDialog返回时崩溃DatePickerDialog
的EditText上和它的OnClickListener:
addBirth.setClickable(true);
addBirth.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
}
});
的onCreateDialog方法和DateSetListener:
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case DATE_DIALOG_ID:
DatePickerDialog dialog = new DatePickerDialog(getApplicationContext(), mDateSetListener, mYear, mMonth, mDay);
return dialog;
}
return null;
}
protected DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
private void updateDisplay() {
addBirth.setText(
new StringBuilder()
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
而这里的logcat的:
04-23 14:46:56.142: D/AndroidRuntime(751): Shutting down VM
04-23 14:46:56.142: W/dalvikvm(751): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-23 14:46:56.241: E/AndroidRuntime(751): FATAL EXCEPTION: main
04-23 14:46:56.241: E/AndroidRuntime(751): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-23 14:46:56.241: E/AndroidRuntime(751): at android.view.ViewRoot.setView(ViewRoot.java:531)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.app.Dialog.show(Dialog.java:241)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.app.DatePickerDialog.show(DatePickerDialog.java:132)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.app.Activity.showDialog(Activity.java:2566)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.app.Activity.showDialog(Activity.java:2524)
04-23 14:46:56.241: E/AndroidRuntime(751): at com.android.yawus.phone.AddContact$2.onClick(AddContact.java:65)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.view.View.performClick(View.java:2485)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.view.View$PerformClick.run(View.java:9080)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.os.Handler.handleCallback(Handler.java:587)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.os.Looper.loop(Looper.java:123)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-23 14:46:56.241: E/AndroidRuntime(751): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 14:46:56.241: E/AndroidRuntime(751): at java.lang.reflect.Method.invoke(Method.java:507)
04-23 14:46:56.241: E/AndroidRuntime(751): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-23 14:46:56.241: E/AndroidRuntime(751): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-23 14:46:56.241: E/AndroidRuntime(751): at dalvik.system.NativeStart.main(Native Method)
2012-04-23
Jason L