gpt4 book ai didi

Android - onCreateDialog 在返回 DatePickerDialog 时崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 12:22:56 25 4
gpt4 key购买 nike

我目前正在尝试在单击某个 EditText 时引发 DatePickerDialog。但是,每次单击 EditText 时,应用程序都会崩溃。这是相关代码和LogCat。

EditText 及其 OnClickListener:

    addBirth.setClickable(true);
addBirth.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
}
});

onCreateDialog 方法和 DateSetListener:

@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case DATE_DIALOG_ID:
DatePickerDialog dialog = new DatePickerDialog(getApplicationContext(), mDateSetListener, mYear, mMonth, mDay);
return dialog;
}
return null;
}

protected DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {

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

}
};
private void updateDisplay() {
addBirth.setText(
new StringBuilder()
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}

这是 LogCat:

04-23 14:46:56.142: D/AndroidRuntime(751): Shutting down VM
04-23 14:46:56.142: W/dalvikvm(751): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-23 14:46:56.241: E/AndroidRuntime(751): FATAL EXCEPTION: main
04-23 14:46:56.241: E/AndroidRuntime(751): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-23 14:46:56.241: E/AndroidRuntime(751): at android.view.ViewRoot.setView(ViewRoot.java:531)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.app.Dialog.show(Dialog.java:241)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.app.DatePickerDialog.show(DatePickerDialog.java:132)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.app.Activity.showDialog(Activity.java:2566)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.app.Activity.showDialog(Activity.java:2524)
04-23 14:46:56.241: E/AndroidRuntime(751): at com.android.yawus.phone.AddContact$2.onClick(AddContact.java:65)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.view.View.performClick(View.java:2485)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.view.View$PerformClick.run(View.java:9080)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.os.Handler.handleCallback(Handler.java:587)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.os.Looper.loop(Looper.java:123)
04-23 14:46:56.241: E/AndroidRuntime(751): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-23 14:46:56.241: E/AndroidRuntime(751): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 14:46:56.241: E/AndroidRuntime(751): at java.lang.reflect.Method.invoke(Method.java:507)
04-23 14:46:56.241: E/AndroidRuntime(751): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-23 14:46:56.241: E/AndroidRuntime(751): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-23 14:46:56.241: E/AndroidRuntime(751): at dalvik.system.NativeStart.main(Native Method)

最佳答案

切勿使用 getApplicationContext() 创建对话框。

试试这个。

DatePickerDialog dialog = new DatePickerDialog(YourActivity.this, mDateSetListener, mYear, mMonth, mDay);

关于Android - onCreateDialog 在返回 DatePickerDialog 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10282890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com