应用程序崩溃,而不是弹出日期选择器对话框

时间:2016-08-20 21:37:19

标签: android datepicker calendar android-edittext datepickerdialog

我希望在单击EditText时弹出日期选择器对话框。我已经关注了一些SO帖子和教程,但没有弹出任何内容,然后应用程序崩溃了一段时间。

我不确定这是否是正确的方法,或者我是否遗漏了某些东西。任何帮助表示赞赏!

到目前为止我的代码,Activity.java onCreate:

inputDate = (EditText) findViewById(R.id.inputDate);
    calendar = Calendar.getInstance();

    final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            // TODO Auto-generated method stub
            calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, monthOfYear);
            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateLabel();
        }

    };

    inputDate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new DatePickerDialog(getApplicationContext(), date, calendar
                    .get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                    calendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });

updateLabel:

private void updateLabel() {

    String myFormat = "MM/dd/yy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    inputDate.setText(sdf.format(calendar.getTime()));
}

的xml:

<EditText
                    android:id="@+id/inputDate"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="mm / dd/ yyyy"
                    android:imeOptions="actionDone"
                    android:focusable="false"
                    />

错误logcat:

08-20 17:45:01.082 4892-4892/? E/MotionRecognitionManager: mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@224e570
08-20 17:45:01.092 4892-4892/? E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@c3e00e9
08-20 17:45:01.092 4892-4892/? E/MotionRecognitionManager: motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@c3e00e9
08-20 17:45:01.122 4892-4892/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.bluefirelabs.apptiva, PID: 4892
                                                 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
                                                     at android.view.ViewRootImpl.setView(ViewRootImpl.java:857)
                                                     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:337)
                                                     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
                                                     at android.app.Dialog.show(Dialog.java:350)
                                                     at com.bluefirelabs.apptiva.UserInfoActivity$7.onClick(UserInfoActivity.java:125)
                                                     at android.view.View.performClick(View.java:5702)
                                                     at android.widget.TextView.performClick(TextView.java:10885)
                                                     at android.view.View$PerformClick.run(View.java:22533)
                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                     at android.os.Looper.loop(Looper.java:158)
                                                     at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
08-20 17:45:01.142 3429-4950/? E/android.os.Debug: ro.product_ship = true
08-20 17:45:01.142 3429-4950/? E/android.os.Debug: ro.debug_level = 0x4f4c
08-20 17:45:01.142 3429-4950/? E/android.os.Debug: sys.mobilecare.preload = false

1 个答案:

答案 0 :(得分:1)

这是我的,它运作良好:

<div class="mobile-header">

  <div class="meta">

    <div href="#" class="hamburger">
      <div class="hamburger-line"></div>
      <div class="hamburger-line"></div>
      <div class="hamburger-line"></div>
    </div>


    <img src="www.example.com/image" />

  </div>

  <div class="mobile-navigation open">
    <ul>
      <li><a href="http://localhost:8888/">Overview</a>
      </li>
      <li><a href="http://localhost:8888/blog/">Blog</a>
      </li>
      <li><a href="http://localhost:8888/about/">About</a>
      </li>
      <li><a href="http://localhost:8888/contact/">Contact</a>
      </li>
    </ul>
  </div>

</div>