开发者

How to show dialog from dialog in Android?

开发者 https://www.devze.com 2023-03-29 03:09 出处:网络
My 开发者_C百科application show save or cancel dialog . When user click save , a new dialog will apperars with edit text , date buttonand save button . When user click date button Date dialog will app

My 开发者_C百科application show save or cancel dialog . When user click save , a new dialog will apperars with edit text , date button and save button . When user click date button Date dialog will appear . But I click date button I get InvocationTargetException . How can I solve this ?

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 Method like this

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


I believe you will hit a InvocationTargetException whenever onCreateDialog returns null.

I think the fix is just to make sure you call super.onCreateDialog(id), e.g.

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

To be sure exactly what is going on you should probably add some logs to your code so you can see what method is called before the exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消