Android DatePicker问题和后退按钮

时间:2011-06-28 11:04:37

标签: android datepicker

我有3个日期选择器的活动。问题是,在将所需的日期值设置到我的表单并单击后面后,再次显示Datepicker对话框。我点击取消按钮再次点击返回,我有一个FC。 来自Logcat:

06-28 09:28:57.531: ERROR/AndroidRuntime(2029): java.lang.IllegalArgumentException:       Activity#onCreateDialog did not create a dialog for id 2
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at  android.app.Activity.createDialog(Activity.java:871)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.app.Activity.showDialog(Activity.java:2483)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.util.DialogManager.showDialogInfo(DialogManager.java:63)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.util.ShowConfirmDialogRunnable.run(ShowConfirmDialogRunnable.java:53)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.util.DelegateRunnable.invokePostAndWait(DelegateRunnable.java:59)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.ui.ScreenManager.invokePostAndWait(ScreenManager.java:556)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.ui.ScreenManager.showConfirmDialog(ScreenManager.java:720)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.ui.ScreenManager.exitScreenWithCheck(ScreenManager.java:1046)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at mcrm.android.ui.NewOpptyScreenActivityHelper.onClose(NewOpptyScreenActivityHelper.java:29)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at mcrm.android.ui.NewOpptyScreenActivity.onKeyDown(NewOpptyScreenActivity.java:497)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.view.KeyEvent.dispatch(KeyEvent.java:1037)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.app.Activity.dispatchKeyEvent(Activity.java:2043)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1631)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2368)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2338)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1641)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.os.Looper.loop(Looper.java:123)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.util.ShowConfirmDialogRunnable.run(ShowConfirmDialogRunnable.java:56)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.util.DelegateRunnable.invokePostAndWait(DelegateRunnable.java:59)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.ui.ScreenManager.invokePostAndWait(ScreenManager.java:556)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.ui.ScreenManager.showConfirmDialog(ScreenManager.java:720)
06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.ui.ScreenManager.exitScreenWithCheck(ScreenManager.java:1046)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at mcrm.android.ui.NewOpptyScreenActivityHelper.onClose(NewOpptyScreenActivityHelper.java:29)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at mcrm.android.ui.NewOpptyScreenActivity.onKeyDown(NewOpptyScreenActivity.java:497)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.view.KeyEvent.dispatch(KeyEvent.java:1037)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.app.Activity.dispatchKeyEvent(Activity.java:2043)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1631)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2368)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2338)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1641)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.os.Handler.dispatchMessage(Handler.java:99)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.os.Looper.loop(Looper.java:123)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.util.ShowAlertDialogRunnable.run(ShowAlertDialogRunnable.java:37)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.octanewave.platform.android.util.DelegateRunnable.run(DelegateRunnable.java:19)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.os.Handler.handleCallback(Handler.java:587)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.os.Handler.dispatchMessage(Handler.java:92)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.os.Looper.loop(Looper.java:123)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at android.app.ActivityThread.main(ActivityThread.java:4363)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at java.lang.reflect.Method.invokeNative(Native Method)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at java.lang.reflect.Method.invoke(Method.java:521)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 06-28 09:28:57.531: ERROR/AndroidRuntime(2029):     at dalvik.system.NativeStart.main(Native Method)

以下是代码的一部分:

mPickDate = (ImageView) findViewById(R.id.pickDate);
  mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         showDialog(DATE_DIALOG_ID);
        }
    });

@Override
protected Dialog onCreateDialog(int id) {
 switch (id) {
 case DATE_DIALOG_ID:
     return new DatePickerDialog(this,
                 mDateSetListener,
                 mYear, mMonth, mDay);
 case DATE_DIALOG_ID_RETURN:
     return new DatePickerDialog(this,
       mDateSetListenerreturn,
                 mYear, mMonth, mDay);     
 }
 return null;
}

 // updates the date in the TextView
    private void updateDisplay(TextView mDateDisplay) {
    mDateDisplay.setText(
        new StringBuilder()
                // Month is 0 based so add 1
               .append(mDay).append("-")
               .append(mMonth + 1).append("-")
               .append(mYear).append("")


                );
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
    new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, 
                              int monthOfYear, int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            updateDisplay(mDateDisplay);
        }
    };

...

我在onCreateDialog()中使用'getParent()'更改了'this',但没有任何反应 有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您的错误发生在DialogManager.java的第63行。在没有看到代码的情况下,我不能再具体了,但看起来你的showDialog()的一个参数存在问题。

您已按照here所述覆盖了onCreateDialog()方法,如果是,则在给定id = 2时创建一个对话框,不是吗?