我想在點擊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 = [email protected]
08-20 17:45:01.092 4892-4892/? E/MotionRecognitionManager: motionService = [email protected]c3e00e9
08-20 17:45:01.092 4892-4892/? E/MotionRecognitionManager: motionService = [email protected]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
日誌說什麼? :D – XxGoliathusxX
更新了帖子,錯誤logcat – skbrhmn