Android - onCreateDialog在返回DatePickerDialog时崩溃

时间:2012-04-23 14:52:08

标签: android android-dialog android-datepicker

我正在尝试在单击某个EditText时引发DatePickerDialog。但是,每次单击EditText时,应用程序都会崩溃。这是相关的代码和LogCat。

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)

2 个答案:

答案 0 :(得分:7)

永远不要使用getApplicationContext()来创建对话框。

试试这个。

DatePickerDialog dialog = new DatePickerDialog(YourActivity.this, mDateSetListener, mYear, mMonth, mDay);

答案 1 :(得分:0)

她是官方Google门票,可解释您的问题:http://code.google.com/p/android/issues/detail?id=11199