之前使用的一直很顺利,但是在写最近的一个Activity时MyAdapter却抛出了ArrayIndexOutOfBoundException,出错的行是继承SimpleAdapter的MyAdapter中的getView方法,如下:
View v=super.getView(position, convertView, parent)
因为代码已经被我删掉了,所以恕不能提供完整代码。。。但是这里令我很困惑,这里出错难道不是意味着SimpleAdapter内部出错吗?这怎么可能。。。
所以我把MyAdapter去掉,改成直接使用SimpleAdapter,代码如下:
ListView orderView=(ListView)findViewById(R.id.listView4);
SimpleAdapter adapter = new SimpleAdapter(this, getData("all"), R.layout.item_order, new String[]{"name,status,time,count,price"}, new int[]{R.id.textView34,R.id.textView38,R.id.textView35,R.id.textView36,R.id.textView37});
orderView.setAdapter(adapter);
这回倒好,抛出来了一个ArrayIndexOutOfBoundException
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:160)
at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16628)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16628)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2298)
at android.view.View.measure(View.java:16628)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5622)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
但是奇怪的是这里面没有声明哪一行代码抛出了exception,这我还是首次见到。。。
我的问题就是在这里,同样的代码之前在别的Activity中运行的非常好,一点错误也没有,我也没有修改过任何文件,为什么换了一个Activity就会抛出这种Exception?而且这么奇葩的错误该如何解决?
更奇怪的是当我发现这个错误后,再回去运行之前写好的Activity时,发现居然之前的Activity也不能正常运行了。。。虽然没有抛出任何错误,但是ListView本该正常加载的item现在都不显示了。。。这是什么奇葩情况啊?求大神帮忙解答。。。。
感激不尽!
4 个解决方案
#1
附上getData方法的代码
public List<Map<String,Object>> getData(String type){
List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
Map<String,Object> order=new HashMap<String,Object>();
order.put("name",“ItemA");
order.put("status","Complete");
order.put("time","2018/4/6");
order.put("count","1");
order.put("price","$5.32");
data.add(order);
return data;
}
#2
getData返回一个空list的话倒是解决了问题。。。
#3
new String[]{"name,status,time,count,price"},
这个初始化的有问题把,应该是
new String[]{"name","status","time","count","price"},
这个初始化的有问题把,应该是
new String[]{"name","status","time","count","price"},
#4
啊果然是这个问题。。。
突然感觉好丢人,一个粗心导致的问题被我用这么长篇大论描述了半天。。
谢谢!!!
#1
附上getData方法的代码
public List<Map<String,Object>> getData(String type){
List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
Map<String,Object> order=new HashMap<String,Object>();
order.put("name",“ItemA");
order.put("status","Complete");
order.put("time","2018/4/6");
order.put("count","1");
order.put("price","$5.32");
data.add(order);
return data;
}
#2
getData返回一个空list的话倒是解决了问题。。。
#3
new String[]{"name,status,time,count,price"},
这个初始化的有问题把,应该是
new String[]{"name","status","time","count","price"},
这个初始化的有问题把,应该是
new String[]{"name","status","time","count","price"},
#4
啊果然是这个问题。。。
突然感觉好丢人,一个粗心导致的问题被我用这么长篇大论描述了半天。。
谢谢!!!