Android ArrayMap ClassCastException

时间:2016-05-23 03:49:53

标签: android

我在处理程序发送邮件时遇到了问题。

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Object[]
at android.util.ArrayMap.allocArrays(ArrayMap.java:187)
at android.util.ArrayMap.put(ArrayMap.java:456)
at android.os.BaseBundle.putInt(BaseBundle.java:389)
at com.easou.ecom.mads.statistics.b.b(Unknown Source)
at com.easou.ecom.mads.statistics.b.a(Unknown Source)
at com.easou.ecom.mads.statistics.b.e(Unknown Source)
at com.easou.ecom.mads.AdSwitchLayout.j(Unknown Source)
at com.easou.ecom.mads.AdSwitchLayout.b(Unknown Source)
at com.easou.ecom.mads.AdSwitchLayout$a.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5539)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

谁能帮帮我?非常感谢..

这是我的java代码:

 try {
        Message message = mHandler.obtainMessage(MESSAGE_ACTION_COUNT);
        message.arg1 = event;
        Bundle bundle = new Bundle();
        bundle.putInt(KEY_MESSAGE_ID, id);
        bundle.putInt(KEY_MESSAGE_TYPE, type);
        bundle.putString(KEY_MESSAGE_PID, publisherId);
        message.setData(bundle);
        return message;
    } catch (Exception e){
        return null;
    }

1 个答案:

答案 0 :(得分:2)

当ArrayMap尝试扩容其容量时,会出现问题。 这可能与已知问题AOSP Issue 218944

有关

解决此问题的方法是确保ArrayMap具有足够的初始容量。 因此,尝试在Bundle的约束器中根据需要特定容量。

Message message = mHandler.obtainMessage(MESSAGE_ACTION_COUNT);
message.arg1 = event;
Bundle bundle = new Bundle(3);
bundle.putInt(KEY_MESSAGE_ID, id);
bundle.putInt(KEY_MESSAGE_TYPE, type);
bundle.putString(KEY_MESSAGE_PID, publisherId);
message.setData(bundle);
return message;

遵循Bundle源代码以确保您使用正确的initialCapacity创建了ArrayMap。

/**
 * Constructs a new, empty Bundle sized to hold the given number of
 * elements. The Bundle will grow as needed.
 *
 * @param capacity the initial capacity of the Bundle
 */
public Bundle(int capacity) {
    super(capacity);
    mFlags = FLAG_HAS_FDS_KNOWN | FLAG_ALLOW_FDS;
}

导致BaseBundle

/**
 * Constructs a new, empty Bundle sized to hold the given number of
 * elements. The Bundle will grow as needed.
 *
 * @param capacity the initial capacity of the Bundle
 */
BaseBundle(int capacity) {
    this((ClassLoader) null, capacity);
}

接下来是真正的影响

/**
 * Constructs a new, empty Bundle that uses a specific ClassLoader for
 * instantiating Parcelable and Serializable objects.
 *
 * @param loader An explicit ClassLoader to use when instantiating objects
 * inside of the Bundle.
 * @param capacity Initial size of the ArrayMap.
 */
BaseBundle(@Nullable ClassLoader loader, int capacity) {
    mMap = capacity > 0 ?
            new ArrayMap<String, Object>(capacity) : new ArrayMap<String, Object>();
    mClassLoader = loader == null ? getClass().getClassLoader() : loader;
}