android ArrayMap抛出java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.Object []

时间:2016-09-06 07:45:32

标签: java android

我使用ArrayMap<String, ArrayList<Object>> group = new ArrayMap<>();

像这样

ArrayMap.put(key,value)

但我致电ClassCastException

allocArrays(final int size)方法中抛出 private void allocArrays(final int size) { if (size == (BASE_SIZE*2)) { synchronized (ArrayMap.class) { if (mTwiceBaseCache != null) { final Object[] array = mTwiceBaseCache; mArray = array; mTwiceBaseCache = (Object[])array[0]; mHashes = (int[])array[1]; array[0] = array[1] = null; mTwiceBaseCacheSize--; if (DEBUG) Log.d(TAG, "Retrieving 2x cache " + mHashes + " now have " + mTwiceBaseCacheSize + " entries"); return; } } } else if (size == BASE_SIZE) { synchronized (ArrayMap.class) { if (mBaseCache != null) { final Object[] array = mBaseCache; mArray = array; mBaseCache = (Object[])array[0]; mHashes = (int[])array[1]; array[0] = array[1] = null; mBaseCacheSize--; if (DEBUG) Log.d(TAG, "Retrieving 1x cache " + mHashes + " now have " + mBaseCacheSize + " entries"); return; } } } mHashes = new int[size]; mArray = new Object[size<<1]; }

mBaseCache = (Object[])array[0];

应用程序崩溃此行@DatabaseField(defaultValue = "0") Integer intValue;

非常感谢!!!

3 个答案:

答案 0 :(得分:2)

问题的原因是多线程。

修复此错误https://issuetracker.google.com/issues/37114373 更新支持V25.4.0已修复

答案 1 :(得分:0)

这似乎是由此问题引起的:https://code.google.com/p/android/issues/detail?id=218944

我在android框架代码本身内部的随机位置得到了相同的异常,所以在这些情况下没有什么可以做的。

答案 2 :(得分:0)

这是解决此问题的一种方法:

    private void fixArrayMapIssue() {
            try {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                    Field mBaseCacheSize = ArrayMap.class.getDeclaredField("mBaseCacheSize");
                    mBaseCacheSize.setAccessible(true);
                    mBaseCacheSize.set(null, 100);

                    Field mTwiceBaseCacheSize = ArrayMap.class.getDeclaredField("mTwiceBaseCacheSize");
                    mTwiceBaseCacheSize.setAccessible(true);
                    mTwiceBaseCacheSize.set(null, 100);
                }
            } catch (Throwable e) {
                e.printStackTrace();
            }
    }

在应用程序onCreate

上运行上述方法