单击按钮后,我试图显示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$3.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)
这是活动的代码:
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);
用途:活动上下文
DatePickerDialog datePickerDialog = new DatePickerDialog(
reservar_restaurant.this, reservar_restaurant.this, mYear, mMonth, mMinute);