android - 调用 Dialog.show() 时出现 ArrayIndexOutOfBoundsException

标签 android android-theme

我在 Samsung Galaxy S II (GT-I9100G) 和 Samsung NTT DOCOMO Galaxy S II LTE (SC-03D) 上看到了这一点。

我正在针对 14 级构建,目标是 10 级(最低 3 级)。

我认为这与主题有关,但不知道从哪里开始。我很确定这不会发生在所有 Galaxy SII 上,否则我会收到更多报告。

或者可能在调用此代码时 Activity 已被销毁 - 这会导致它吗?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.app_icon);
builder.setTitle("my title");
builder.setMessage("my message which has about 7 lines and each line is short");
builder.setPositiveButton(R.string.ok, null);
builder.show();

日志猫:

java.lang.ArrayIndexOutOfBoundsException: null
 at android.content.res.TypedArray.getInt(TypedArray.java:248)
 at android.widget.ScrollView.<init>(ScrollView.java:173)
 at android.widget.ScrollView.<init>(ScrollView.java:161)
 at java.lang.reflect.Constructor.constructNative(Constructor.java:-2)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
 at android.view.LayoutInflater.createView(LayoutInflater.java:505)
 at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
 at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:215)
 at com.android.internal.app.AlertController.installContent(AlertController.java:215)
 at android.app.AlertDialog.onCreate(AlertDialog.java:251)
 at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
 at android.app.Dialog.show(Dialog.java:225)
 at android.app.AlertDialog$Builder.show(AlertDialog.java:823)

最佳答案

导致异常的代码应该是:

TypedArray.getInt (TypedArray.java:248)

/**
 * Retrieve the integer value for the attribute at <var>index</var>.
 * 
 * @param index Index of attribute to retrieve.
 * @param defValue Value to return if the attribute is not defined.
 * 
 * @return Attribute int value, or defValue if not defined.
 */
public int getInt(int index, int defValue) {
    index *= AssetManager.STYLE_NUM_ENTRIES;
    final int[] data = mData;
    final int type = data[index+AssetManager.STYLE_TYPE];
    if (type == TypedValue.TYPE_NULL) {
        return defValue;
    } else if (type >= TypedValue.TYPE_FIRST_INT
        && type <= TypedValue.TYPE_LAST_INT) {
        // vvvvvvvvvvvvvvvvvvvvvv 248
        return data[index+AssetManager.STYLE_DATA];
        // ^^^^^^^^^^^^^^^^^^^^^^ 248
    }

    TypedValue v = mValue;
    if (getValueAt(index, v)) {
        Log.w(Resources.TAG, "Converting to int: " + v);
        return XmlUtils.convertValueToInt(
            v.coerceToString(), defValue);
    }
    Log.w(Resources.TAG, "getInt of bad type: 0x"
          + Integer.toHexString(type));
    return defValue;
}

奇怪的是,在ScrollView.java中没有getInt()(为什么那个Dialog中连一个ScrollView都没有?)。这个异常(exception)看起来也不寻常。您通常会得到 Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=12345 而不是 null

不知道哪里出了问题。但它实际上可能是由一些自定义布局引起的,因为 getInt 通常会像这样查询 View 属性

case R.styleable.View_scrollbars:
    final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
    if (scrollbars != SCROLLBARS_NONE) {
        viewFlagValues |= scrollbars;

我会说这是三星的自定义固件或某些软件修改导致的。

关于android - 调用 Dialog.show() 时出现 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9755309/

相关文章:

android - 具有自定义颜色属性的 TextView 不会在布局预览中呈现

android - 有多个闹钟时取消单个闹钟

android - 如何在 GridView 中从数据库中一张一张地加载图像?

android - 如何在 Android 中创建 TreeView ?

android - 如何在 quora 中的编辑文本末尾添加问号?

Android/Eclipse 应用主题时出现问题

java - 如何仅使用 XML 在 Android 中显示谷歌广告?

java - ActionBar 仅适用于 ThemeOverlay

android - 更改 Material 组件样式属性失败

android - 以编程方式将主题颜色更改为 Android 中的任何颜色