应用程序崩溃而不是弹出日期选择器对话框
App crashes instead of Date Picker dialog popping up
我希望在单击 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();
}
});
更新标签:
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.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
那是我的,效果很好:
DatePickerDialogdatePickerDialog = new DatePickerDialog(this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//Magic happens here
}
}, nowYear, nowMonth, nowDay);
datePickerDialog.show();
我希望在单击 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();
}
});
更新标签:
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.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
那是我的,效果很好:
DatePickerDialogdatePickerDialog = new DatePickerDialog(this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//Magic happens here
}
}, nowYear, nowMonth, nowDay);
datePickerDialog.show();