如何在Android中显示对话框中的对话框?

时间:2011-08-17 06:21:15

标签: android

我的应用程序显示保存或取消对话框。当用户单击“保存”时,将出现一个带有编辑文本,日期按钮和保存按钮的新对话框。当用户单击日期按钮时,将出现日期对话框。但我点击日期按钮我得到InvocationTargetException。我该如何解决这个问题?

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;
    }

1 个答案:

答案 0 :(得分:0)

我相信只要InvocationTargetException返回null,您就会点击onCreateDialog

我认为修复只是为了确保您拨打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);
}

为确保发生了什么,您应该在代码中添加一些日志,以便在异常之前查看调用的方法。