Android:DatePicker在Activity内部不起作用

时间:2010-10-20 10:25:10

标签: android datepicker

在我的应用程序中,我有一个Tabbar,我使用ActivityGroup将内容加载到每个选项卡中,如下所示。

public class FirstGroup extends ActivityGroup {

        // Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view
 public static FirstGroup group;

        // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
 private ArrayList<View> history;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       this.history = new ArrayList<View>();
       group = this;

              // Start the root activity withing the group and get its view
       View view = getLocalActivityManager().startActivity("FlightsActivity", new
                   Intent(this,FlightsActivity.class)
                   .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                                      .getDecorView();

              // Replace the view of this ActivityGroup
       replaceView(view);

    }

 public void replaceView(View v) {
                // Adds the old one to history
  history.add(v);
                // Changes this Groups View to the new View.
  setContentView(v);
 }

我在FlightsActivity活动类中有一个图像,在图像的onClick事件上我需要显示一个日期选择器。我已经编写了代码,当我直接指定FlightsActivity作为内容时,它工作正常选项卡而不是通过ActivityGroup加载它。但现在我收到错误

10-20 14:11:16.302: ERROR/AndroidRuntime(294): FATAL EXCEPTION: main
10-20 14:11:16.302: ERROR/AndroidRuntime(294): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@43e497e0 is not valid; is your activity running?
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.view.ViewRoot.setView(ViewRoot.java:505)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.app.Dialog.show(Dialog.java:241)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.app.DatePickerDialog.show(DatePickerDialog.java:129)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.app.Activity.showDialog(Activity.java:2556)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.app.Activity.showDialog(Activity.java:2514)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at tabviewapp.com.FlightsActivity$10.onClick(FlightsActivity.java:166)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.view.View.performClick(View.java:2408)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.view.View$PerformClick.run(View.java:8816)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.os.Handler.handleCallback(Handler.java:587)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.os.Looper.loop(Looper.java:123)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at android.app.ActivityThread.main(ActivityThread.java:4627)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at java.lang.reflect.Method.invoke(Method.java:521)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-20 14:11:16.302: ERROR/AndroidRuntime(294):     at dalvik.system.NativeStart.main(Native Method)

以下是我实现datepicker的代码:

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);
            }
        };

        private DatePickerDialog.OnDateSetListener mDateSetListenerreturn =
            new DatePickerDialog.OnDateSetListener() {

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

3 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

尝试此操作以显示您的context

而不是:

setContentView(R.layout.xyz);

使用此:

View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.xyz, null);
this.setContentView(viewToLoad );       

答案 2 :(得分:-1)

这应该有效:

private void setInputDatePickerDialog(Activity activity) {

    dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

    Calendar newCalendar = Calendar.getInstance();
    inputDatePickerDialog = new DatePickerDialog(activity, //<-- this is the best