2016-05-23 84 views
0

當我通過處理程序發送消息時遇到問題。Android ArrayMap ClassCastException

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; 
    } 
+0

我很懷疑這是正在生成異常的代碼,因爲你是默默捕獲所有異常並返回'null'。 (您至少應該記錄任何捕獲的異常。)您是否通過'retrace'運行堆棧跟蹤來解碼混淆的方法名稱? –

+0

@kkk我建議你在調試時禁用你的代碼縮小,以便你確切地知道哪一行發生了錯誤。 –

回答

1

即使世界都出現了一些問題,當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; 

按照包的源代碼,以確保你已經創建ArrayMap右參數:initialCapacity。

/** 
* 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; 
}