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

跟風遠走 提交于 2019-12-20 03:56:07

问题


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

回答1:


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...




回答2:


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;



回答3:


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.




回答4:


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



回答5:


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

Instead use this.



来源:https://stackoverflow.com/questions/6259644/android-java-lang-illegalargumentexception-erron-when-creating-dialog-on-2-1-a

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