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

前端 未结 5 1272
既然无缘
既然无缘 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: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...

提交回复
热议问题