Android - java.lang.IllegalArgumentException 在 2.1 和低 androi

时间:2023-04-24
本文介绍了Android - java.lang.IllegalArgumentException 在 2.1 和低 android 上创建对话框时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我从 SDK 版本的手机收到以下错误消息 <8. 我刚刚在安卓市场上发布了这个应用程序,在发布之前我的测试手机是 HTC Thunderbolt 和 Droid X.根本没有这个问题.

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.

我通过 Flurry 收到此错误报告.我无法直接对此进行测试,因为我没有带有 SDK < 的手机8 并且由于某种原因,我无法让我的模拟器启动低于为应用程序设置的默认 SDK 的版本.

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 没有为 id 1 创建对话框)

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

下面是我实现的 onCreateDialog(int id).

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

推荐答案

也遇到了这个问题,用下面的方法解决了:

Had the problem also and solved it with the following:

-不要在方法中返回空对话框:protected Dialog onCreateDialog(int id)

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

在 Android 2.1 中的 Activity.java 中,第 871 行引发了错误.

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     }

如果您在以后的 Android 中查看,则会检查空 Dialog,并且它会返回并处理.所以在这里它起作用了.

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.

-不要使用 Bundle(在 API8 中更改):

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

受保护的对话框 onCreateDialog(int id, Bundle bundle);

protected Dialog onCreateDialog(int id, Bundle bundle);

使用:

受保护的对话框 onCreateDialog(int id);

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

这篇关于Android - java.lang.IllegalArgumentException 在 2.1 和低 android 上创建对话框时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:Android - FEATURE_NO_TITLE 不起作用 下一篇:Android:对话框在 droid 3 上有额外的、丑陋的空间?

相关文章