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

让人想犯罪 __ 提交于 2019-12-02 00:25:33

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!