ArrayMap java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Object[]
错误堆栈:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Object[]
at android.support.v4.util.SimpleArrayMap.allocArrays(SourceFile:183)
at android.support.v4.util.SimpleArrayMap.put(SourceFile:437)
错误原因:
由于SimpleArrayMap 里面使用了一个静态变量的缓存,mBaseCache,
static Object[] mBaseCache;
该变量默认有两个数据,第1个元素是一个object[],用于存放上次的缓存的mBaseCache
第二个元素是int[],用于存在hash。具体赋值代码可以看下面
synchronized (ArrayMap.class) {
if (mBaseCacheSize < CACHE_SIZE) {
array[0] = mBaseCache;
array[1] = hashes;
for (int i=(size<<1)-1; i>=2; i--) {
array[i] = null;
}
mBaseCache = array;
mBaseCacheSize++;
if (DEBUG) Log.d(TAG, "Storing 1x cache " + array
+ " now have " + mBaseCacheSize + " entries");
}
}
使用该数组的地方在:
SimpleArrayMap 的allocArrays 方法里
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;
}
}
下面这段代码是有风险的,如果mBaseCache 在多线程被修改了,就会把ClassCastException 异常。
mBaseCache = (Object[])array[0];
解决方法:
如果项目某个地方报这个错误,请把这个地方的ArrayMap替换成 HasMap. HasMap 多线程不会崩溃,虽然,他不是特别完好的支持。不需要把项目中所有的地方都替换掉,没有必要。单独线程,ArrayMap 完全没有问题。
错误复现:这个复现起来超级麻烦,我花了一周的时间,才找到复现的漏洞,分享给大家:
/**
* 复现该问题 用了四个线程
* java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Object[]
* at android.support.v4.util.SimpleArrayMap.allocArrays(SimpleArrayMap.java:157)
* at android.support.v4.util.SimpleArrayMap.put(SimpleArrayMap.java:399)
* at com.example.fragment.MainFragment$14.run(MainFragment.java:280)
* 1.首先 线程1 执行到put 方法的
* mArray[index<<1] = key;
* mArray[(index<<1)+1] = value;
* mSize++;
* return null;
* 最上面这个位置 目的是让这个数组不再是空的
*
* 2.执行线程2 也执行到
* mArray[index<<1] = key;
* mArray[(index<<1)+1] = value;
* mSize++;
* return null;
* 最上面这个位置 目的是让这个put 的东西,放在第0个位置,因为put里面会生成index,
* 让两个线程都放到index 是0 的位置
*
* 3.把线程1执行完,这样数据里面已经放进去一个数据了
*
* 4.执行线程3 到removeAt 方法的 freeArrays 的 mBaseCache = array; 之前
* public V removeAt(int index) {
* final Object old = mArray[(index << 1) + 1];
* if (mSize <= 1) {
* // Now empty.
* if (DEBUG) Log.d(TAG, "remove: shrink from " + mHashes.length + " to 0");
* freeArrays(mHashes, mArray, mSize);
*
* mBaseCache = array;----------- freeArrays
*
* 这个的目的是调用freeArray 方法,让当前的map释放当前的数组。这样就可以生成mBaseCache了
*
* 5.把线程2 执行完
* 这样就会把mBaseCache 赋值的数组,重新赋值
*
* 6.把线程3执行完
* ok,现在mBaseCache已经被污染了
*
* 7.执行线程4
*
*/
private void CMETestCastException() {
final ArrayMap testArrayMap = new ArrayMap();
final ArrayMap testArrayMap2 = new ArrayMap();
new Thread("线程1"){
@Override
public void run() {
super.run();
testArrayMap.put("2324","fffff");
}
}.start();
new Thread("线程2"){
@Override
public void run() {
super.run();
testArrayMap.put("test","string");
}
}.start();
new Thread("线程3"){
@Override
public void run() {
super.run();
testArrayMap.removeAt(0);
}
}.start();
new Thread("线程4"){
@Override
public void run() {
super.run();
testArrayMap2.put("aaa","string");
}
}.start();
}
复现这个问题的时候,关键是把mBaseCache 污染掉。这里四个线程的话,需要调试,调试步骤就是上面我注释的。
总结:
如果当前的map 会有多个线程访问,请使用HasMap. 该问题,google 并没有解决。在高版本上,直接扔CME ConcurrentModificationException.
ArrayMap java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Object[]的更多相关文章
- java.lang.ClassCastException: oracle.sql.TIMESTAMP cannot be cast to java.sql.Timestamp
http://stackoverflow.com/questions/13269564/java-lang-classcastexception-oracle-sql-timestamp-cannot ...
- java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
最近在学习drawerLayout时,遇到这个bug.如下示: java.lang.ClassCastException: android.widget.RelativeLayout cannot b ...
- [Ljava.lang.String; cannot be cast to java.lang.String报错的原因
完整错误信息: java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String 报这个错的原因 ...
- java.lang.ClassCastException: android.app.Application cannot be cast to
出这个异常的原因是在项目中添加了新lication类(public class Application extends lication)之后,没有在AndroidManifest.xml中添加该类的 ...
- 关于android使用ksoap2报Caused by: java.lang.ClassCastException: org.ksoap2.SoapFault cannot be cast to org.ksoap2.serialization.SoapObject
Caused by: java.lang.ClassCastException: org.ksoap2.SoapFault cannot be cast to org.ksoap2.serializa ...
- java.lang.ClassCastException: oracle.sql.CLOB cannot be cast to oracle.sql.CLOB
错误现象: [framework] 2016-05-26 11:34:53,590 -INFO [http-bio-8080-exec-7] -1231863 -com.dhcc.base.db.D ...
- java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to android.widget.Ed ...
- 安卓出现错误: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to android.widget.Ed ...
- java.lang.ClassCastException:android.widget.Button cannot be cast to android.widget.ImageView
今天遇到一个错误也不知道怎么回事,上网搜了一下: 出现的问题是:java.lang.ClassCastException:android.widget.Button cannot be cast to ...
随机推荐
- C语言 宏的定义
#include <stdio.h> // NUM叫做宏名 // 6是用来替换宏名的字符串 #define NUM 6 #define mul(a, b) ((a)*(b)) void t ...
- ubuntu git svn 缺少 subversion-perl
在命令行中输入以下命令:sudo apt-get install subversion-tools等待安装成功即可.
- note03-计算机网络
3. 网络层 网络层的主要协议有IP.ICMP.IGMP.ARP等: IP地址分类:ABCDE ,根据32比特位的IP中网络号所占位数进行决定IP的类型 A:0 0000000 网络号| 000000 ...
- Angular 引用库
<!doctype html> <html ng-app> <head> <script src="http://code.angularjs.or ...
- Storm处理流程, 基本参数配置
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAksAAAG/CAYAAABIVpOQAAAABHNCSVQICAgIfAhkiAAAIABJREFUeF
- JSP的小心得
问题:Web容器(例如Tomcat)是怎么来执行jsp文件的? 首先它会将放在webapps目录下的jsp文件(这里以hello.jsp为例)翻译成hello_jsp.java文件并编译为hello_ ...
- C++继承和派生练习(一)--关于从people(人员)类派生出student(学生)类等
. 从people(人员)类派生出student(学生)类 添加属性:班号char classNO[]:从people类派生出teacher(教师)类, 添加属性:职务char principalsh ...
- MySQL数据库初识——初窥MySQL
初步了解MySQL基本数据库语言 1.创建一个Mysql数据库 create database database_name: 2.显示所有的Mysql数据库 show databases: 3.使用 ...
- js | javascript获取和设置元素的属性
获取和设置元素的内容: var nv = document.getElementById("pid"); alert(nv.innerHTML); nv.innerHTML=&qu ...
- 在Liunx上搭建FTP并配置用户权限
伴随着.Net Core的开源,公司前几天上了新的Liunx服务器,我在前几篇文章中介绍了如何搭建环境以及部署.Net Core应用. 然后,今天客户和我说想自己给网站做推广,需要用FTP链接我们的服 ...