2011-08-17 182 views
0

我的應用程序顯示保存或取消對話框。當用戶點擊保存時,一個新的對話框將顯示編輯文本,日期按鈕和保存按鈕。當用戶點擊日期按鈕日期對話框將出現。但我點擊日期按鈕,我得到InvocationTargetException。我該如何解決這個問題?如何在Android中顯示對話框中的對話框?

Dialog d = new Dialog(CameraView.this, R.style.Dialog);    
         d.requestWindowFeature(Window.FEATURE_NO_TITLE); 
         d.setContentView(R.layout.img_info); 
         loadDate(); 
         d.setCancelable(true); 

LoadDate法這樣

private void loadDate(){ 
     // capture our View elements 
     mDateDisplay = (TextView) findViewById(R.id.dateDisplay); 
     mPickDate = (Button) findViewById(R.id.pickDate); 

     // add a click listener to the button 
     mPickDate.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       showDialog(DATE_DIALOG_ID); 
      } 
     }); 

     // get the current date 
     final Calendar c = Calendar.getInstance(); 
     mYear = c.get(Calendar.YEAR); 
     mMonth = c.get(Calendar.MONTH); 
     mDay = c.get(Calendar.DAY_OF_MONTH); 

     // display the current date (this method is below) 
     updateDisplay(); 

    } 
    private void updateDisplay() { 
     mDateDisplay.setText(
      new StringBuilder() 
        // Month is 0 based so add 1 
        .append(mMonth + 1).append("-") 
        .append(mDay).append("-") 
        .append(mYear).append(" ")); 
    } 

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

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

    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DATE_DIALOG_ID: 
      return new DatePickerDialog(this, 
         mDateSetListener, 
         mYear, mMonth, mDay); 
     } 
     return null; 
    } 

回答

0

我相信你會打一個InvocationTargetException每當onCreateDialog返回null。

我認爲修正只是爲了確保您撥打super.onCreateDialog(id),例如

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DATE_DIALOG_ID: 
     return new DatePickerDialog(this, 
        mDateSetListener, 
        mYear, mMonth, mDay); 
    } 
    return super.onCreateDialog(id); 
} 

要確定到底是怎麼回事,你應該可能是一些記錄添加到您的代碼,所以你可以看到什麼方法異常之前被調用。