开发者

Android - java.lang.IllegalArgumentException Erron when creating Dialog on 2.1 and low android

I'm getting the below error message from phones that are SDK version < 8. I just released this app on the android market and prior to release my test phones were a HTC Thunderbolt and the Droid X. Neither had this problem at all.

I'm getting this error report through Flurry. I'm not able to test this directly because I don't have a phone with SDK < 8 and for some reason I can't get my emulator to start a lower version than the default SDK set for an app.

java.lang.IllegalArgumentException, android.app.Activity.createDialog:880 - (Activity#onCreateDialog did not create a dialog for id 1)

Below is the onCreateDialog(int id) that i've implemented.

@Override
protected Dialog onCreateDialog(int id) {
    super.onCreateDialog(id);
    Dialog dialog = null;

    switch(id){
    case 1:
        dialog = new CustomCalcDialog(this);
        dialog.setTitle("Enter Shipping %");
        activeTextView = shippingPercent;
        dialog.show();
        dialog = null;
        break;
    case 2:
        dialog = new CustomCalcDialog(this);
        dialog.setTitle("Enter Tax Rate");
        activeTextView = taxPercent;
        dialog.show();
        dialog = null;
        break;
    case 3:
        dialog = new CustomCalcDialog(this);
        dialog.setTitle("Enter Commission %");
        activeTextView = commissionPercent;
        dialog.show();
        dialog = null;
        break;
    case 4:
        dialog = new CustomCalcDialog(this);
        dialog.setTitle("Calculate Subtotal");
        activeTextView = productSubtotal;
        dialog.show();
        dialog = null;
        break;
    case 5:
        dialog = new CustomCalcDialog(this);
        dialog.setTitle("Additional Shipping");
        activeTextView = addShipping;
        dialog.show();
        dialog = null;
        break;
    case 6:
        dialog = new BackgroundOptionsDialog(this);
        dialog.setTitle("Choose Background:");
        dialog.show();
        dialog = null;
        break;
    default:
        dialog = null;
    }
    return dialog;
}

And below is how the Dialog is being dismissed.

private void registerListeners () {

        enterTotal.setOnClickListener(new View.OnClickListener() {

            @Override
      开发者_StackOverflow中文版      public void onClick(View v) {
                try {
                    calcLogic(EQUALS);
                }catch(Exception ex){}
                operatorChange = DONT_CHANGE;

                activeTextView.setText(calcDialogDisplay.getText().toString());

                try {
                    if ((Float.parseFloat(calcDialogDisplay.getText().toString())) < 0) {}
                }catch(Exception ex) {
                    activeTextView.setText("0");
                }
                mathCalculations();
                CustomCalcDialog.this.dismiss();
            }
        });


Had the problem also and solved it with the following:

-Don't return a null Dialog in method: protected Dialog onCreateDialog(int id)

In Android 2.1 in Activity.java the error was raised on line 871.

private Dialog createDialog(Integer dialogId, Bundle state) {
869         final Dialog dialog = onCreateDialog(dialogId);
870         if (dialog == null) {
871             throw new IllegalArgumentException("Activity#onCreateDialog did "
872                     + "not create a dialog for id " + dialogId);
873         }
874         dialog.dispatchOnCreate(state);
875         return dialog;
876     }

If you look in later Android there is a check for a null Dialog and it's handeld with a return. So here it works.

-Don't use Bundle (changed in API8):

protected Dialog onCreateDialog(int id, Bundle bundle);

use:

protected Dialog onCreateDialog(int id);

-I also used the following check (had a special case with a custom dialog):

    if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR_MR1) {
        return dialog;
    } else {
        return null;
    }

Maybe it helps someone...


Ah, look at your code:

dialog = new CustomCalcDialog(this);
        dialog.setTitle("Enter Shipping %");
        activeTextView = shippingPercent;
        dialog.show();
        dialog = null;
        break;

You set dialog to null: that's why you are getting the error. Do this:

dialog = new CustomCalcDialog(this);
        dialog.setTitle("Enter Shipping %");
        activeTextView = shippingPercent;
        break;


I believe the easiest way to deal with this pesky exception is to just catch it:

@Override
public void showDialog(int dialogId) {
  try {
    super.showDialog(dialogId);
  } catch (IllegalArgumentException e) { /* Log it if you wish*/ }
} 

That said, I don't think you're using onCreateDialog() as the API intended (which is why you were seeing the strange behavior), it should definitely return a dialog object most of the time.


I ended up having to get rid of the dialog = null. I also had to move the switching between activeTextView from onCreateDialog(int id) to onPrepareDialog(int id, Dialog dialog).

Below is the updated code.

    @Override
protected Dialog onCreateDialog(int id) {
    super.onCreateDialog(id);
    Dialog dialog = null;

    switch(id){
    case 1:
        dialog = new CustomCalcDialog(this);
        dialog.setTitle("Enter Shipping %");
        break;
    case 2:
        dialog = new CustomCalcDialog(this);
        dialog.setTitle("Enter Tax Rate");
        break;
    case 3:
        dialog = new CustomCalcDialog(this);
        dialog.setTitle("Enter Commission %");
        break;
    case 4:
        dialog = new CustomCalcDialog(this);
        dialog.setTitle("Calculate Subtotal");
        break;
    case 5:
        dialog = new CustomCalcDialog(this);
        dialog.setTitle("Additional Shipping");
        break;
    case 6:
        dialog = new BackgroundOptionsDialog(this);
        dialog.setTitle("Choose Background:");
        break;
    }
    return dialog;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);

    switch(id){
    case 1:         
        activeTextView = shippingPercent;
        break;
    case 2:
        activeTextView = taxPercent;
        break;
    case 3:
        activeTextView = commissionPercent;
        break;
    case 4:
        activeTextView = productSubtotal;
        break;
    case 5:
        activeTextView = addShipping;
        break;
    }
}


It is because you are returning null from your onCreateDialog(int id) method (this method is deprecated).

Instead use this.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜