2017-05-06 49 views
1

我試圖表現出DatePickerDialog一旦我點擊一個按鈕,但一旦我點擊它,它只是停止應用程序,並會引發以下異常:BadTokenException當我試圖表明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(); 

它可能是什麼?

回答

0

相反的:

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); 
+0

非常感謝!它的工作! –

+0

快樂編碼兄弟... – rafsanahmad007

0

不是傳遞getApplicationContext()的只是傳遞活動情境。

DatePickerDialog datePickerDialog = new DatePickerDialog(
        reservar_restaurant.this, reservar_restaurant.this, mYear, mMonth, mMinute); 

當您在應用程序中顯示對話框時,您需要傳遞作爲活動的上下文。

+0

謝謝!有效 :) –

相關問題