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

前端 未结 5 1229
既然无缘
既然无缘 2021-01-23 04:27

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 Thun

相关标签:
5条回答
  • 2021-01-23 04:29

    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.

    0 讨论(0)
  • 2021-01-23 04:39

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

    0 讨论(0)
  • 2021-01-23 04:39

    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;
    
    0 讨论(0)
  • 2021-01-23 04:41

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-23 04:44

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

    Instead use this.

    0 讨论(0)
提交回复
热议问题