当我试图显示 DatePickerDialog 时出现 BadTokenException
BadTokenException while I am trying to show DatePickerDialog
我试图在单击按钮后显示 DatePickerDialog
,但是一旦单击它,它就会停止应用程序并抛出以下异常:
05-06 08:46:00.688 18786-18786/carloscoronado.chillout E/AndroidRuntime: FATAL EXCEPTION: main
Process: carloscoronado.chillout, PID: 18786
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:694)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:316)
at carloscoronado.chillout.reservar_restaurant.onClick(reservar_restaurant.java:188)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
这是Activity的代码:
public class reservar_restaurant extends AppCompatActivity implements DatePickerDialog.OnDateSetListener{
int mDay = 0;
int mMonth = 0;
int mYear = 0;
int mHour = 0;
int mMinute =0;
Button buttonDate;
Calendar calender = Calendar.getInstance();
DatePickerDialog theDatePickerDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reservar_restaurant);
buttonDate = (Button) findViewById(R.id.btn_fecha);
Calendar c = Calendar.getInstance();
mDay = c.get(Calendar.DAY_OF_MONTH);
mMonth = c.get(Calendar.MONTH);
mYear = c.get(Calendar.YEAR);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
buttonDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//onCreateDialog(DATE_DIALOG_ID);
long actualDay= calender.getTimeInMillis();
final long oneDay = 24 * 60 * 60 * 1000L;
DatePickerDialog datePickerDialog = new DatePickerDialog(
getApplicationContext(), reservar_restaurant.this, mYear, mMonth, mMinute);
datePickerDialog.getDatePicker().setMinDate(actualDay);
datePickerDialog.getDatePicker().setMaxDate(actualDay+(oneDay*7));
datePickerDialog.setTitle("Pick your reservation day");
datePickerDialog.show();
}
});
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
mYear = year;
mMonth = month + 1;
mDay = dayOfMonth;
String sYear = String.valueOf(mYear);
String sMonth = String.valueOf(mMonth);
String sDayOfMonth = String.valueOf(mDay);
if(sMonth.length() <= 1){
sMonth = "0" + sMonth;
}
if (sDayOfMonth.length() <= 1){
sDayOfMonth = "0" + sDayOfMonth;
}
text7.setText(sDayOfMonth + "/" + sMonth + "/" + sYear );
}
}
该异常将我抛至以下代码行:
datePickerDialog.show();
会是什么??
而不是:
DatePickerDialog datePickerDialog = new DatePickerDialog(
getApplicationContext(), reservar_restaurant.this, mYear, mMonth, mMinute);
使用:Activity上下文..
DatePickerDialog datePickerDialog = new DatePickerDialog(
reservar_restaurant.this, reservar_restaurant.this, mYear, mMonth, mMinute);
不传递 getApplicationContext()
而是传递 activity 上下文。
DatePickerDialog datePickerDialog = new DatePickerDialog(
reservar_restaurant.this, reservar_restaurant.this, mYear, mMonth, mMinute);
当您在应用程序中显示对话框时,您需要传递 Activity 上下文。
我试图在单击按钮后显示 DatePickerDialog
,但是一旦单击它,它就会停止应用程序并抛出以下异常:
05-06 08:46:00.688 18786-18786/carloscoronado.chillout E/AndroidRuntime: FATAL EXCEPTION: main
Process: carloscoronado.chillout, PID: 18786
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
at android.view.ViewRootImpl.setView(ViewRootImpl.java:694)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:316)
at carloscoronado.chillout.reservar_restaurant.onClick(reservar_restaurant.java:188)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
这是Activity的代码:
public class reservar_restaurant extends AppCompatActivity implements DatePickerDialog.OnDateSetListener{
int mDay = 0;
int mMonth = 0;
int mYear = 0;
int mHour = 0;
int mMinute =0;
Button buttonDate;
Calendar calender = Calendar.getInstance();
DatePickerDialog theDatePickerDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reservar_restaurant);
buttonDate = (Button) findViewById(R.id.btn_fecha);
Calendar c = Calendar.getInstance();
mDay = c.get(Calendar.DAY_OF_MONTH);
mMonth = c.get(Calendar.MONTH);
mYear = c.get(Calendar.YEAR);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
buttonDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//onCreateDialog(DATE_DIALOG_ID);
long actualDay= calender.getTimeInMillis();
final long oneDay = 24 * 60 * 60 * 1000L;
DatePickerDialog datePickerDialog = new DatePickerDialog(
getApplicationContext(), reservar_restaurant.this, mYear, mMonth, mMinute);
datePickerDialog.getDatePicker().setMinDate(actualDay);
datePickerDialog.getDatePicker().setMaxDate(actualDay+(oneDay*7));
datePickerDialog.setTitle("Pick your reservation day");
datePickerDialog.show();
}
});
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
mYear = year;
mMonth = month + 1;
mDay = dayOfMonth;
String sYear = String.valueOf(mYear);
String sMonth = String.valueOf(mMonth);
String sDayOfMonth = String.valueOf(mDay);
if(sMonth.length() <= 1){
sMonth = "0" + sMonth;
}
if (sDayOfMonth.length() <= 1){
sDayOfMonth = "0" + sDayOfMonth;
}
text7.setText(sDayOfMonth + "/" + sMonth + "/" + sYear );
}
}
该异常将我抛至以下代码行:
datePickerDialog.show();
会是什么??
而不是:
DatePickerDialog datePickerDialog = new DatePickerDialog(
getApplicationContext(), reservar_restaurant.this, mYear, mMonth, mMinute);
使用:Activity上下文..
DatePickerDialog datePickerDialog = new DatePickerDialog(
reservar_restaurant.this, reservar_restaurant.this, mYear, mMonth, mMinute);
不传递 getApplicationContext()
而是传递 activity 上下文。
DatePickerDialog datePickerDialog = new DatePickerDialog(
reservar_restaurant.this, reservar_restaurant.this, mYear, mMonth, mMinute);
当您在应用程序中显示对话框时,您需要传递 Activity 上下文。