我的代码在某些时候抛出一个IndexOutOfBounds异常(无法弄清楚在哪里)。下面的例外日志:
12-09 15:03:59.962: ERROR/AndroidRuntime(8735): FATAL EXCEPTION: main
Process: br.com.app, PID: 8735
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:225)
at android.widget.AbsListView.obtainView(AbsListView.java:2240)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListView.onMeasure(ListView.java:1175)
at android.view.View.measure(View.java:16458)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:16458)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16458)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:16458)
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:2289)
at android.view.View.measure(View.java:16458)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
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:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
正如您所看到的,此日志不会显示我自己编写的任何课程。为了更好地了解正在发生的事情,这里是我的应用程序在这一点上做的事情: - 发出http请求以从服务器获取数据数组 - 我使用这个数组来填充ListView,用户可以选择几个选项
经过几次测试后,我注意到只有当我从服务器获取的jsonArray有1到7个元素时才会出现此错误。如果特定搜索返回,比方说0,7,40项,一切正常。当我只获得一个元素或4个元素时,应用程序崩溃了......根本不知道为什么会发生这种情况......
我收到服务器的响应后立即执行以下方法:
private void didDownload(JSONObject result) {
try {
listingsArray = concatArray(listingsArray, result.getJSONArray("empresas"));
Location currentLocation = PreferenceData.getUserLocation(myContext);
for(int i = 0, j = listingsArray.length(); i < j; i++){
JSONObject c = listingsArray.getJSONObject(i);
JSONObject listing = c.getJSONObject("listing");
double listingLatitude = listing.getDouble("latitude");
double listingLongitude = listing.getDouble("longitude");
Location listingLocation = new Location("");
listingLocation.setLongitude(listingLongitude);
listingLocation.setLatitude(listingLatitude);
float distance = currentLocation.distanceTo(listingLocation);
listingsArray.getJSONObject(i).put("distancia", distance);
}
imageURLs = new String[listingsArray.length()];
for(int i = 0; i < listingsArray.length(); i++) {
String url = ImageUrlBuilder.buildURL(listingsArray.getJSONObject(i).getString("thumb"), myContext, "thumb");
imageURLs[i] = url;
}
} catch (JSONException e) {
Log.e(TAG, "Erro no Json");
}
tamanho = listingsArray.length();
cacheUpdated();
}
在调试时,一步一步,下面的方法是崩溃前我可以遵循的最后一个方法(这个在工作线程中运行,从另一个类运行,将图像加载到我的listview项目中)
class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad;
PhotosLoader(PhotoToLoad photoToLoad){
this.photoToLoad=photoToLoad;
}
@Override
public void run() {
try{
if(imageViewReused(photoToLoad))
return;
Bitmap bmp=getBitmap(photoToLoad.url);
memoryCache.put(photoToLoad.url, bmp);
if(imageViewReused(photoToLoad))
return;
BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad);
handler.post(bd); //here is where I can't debug further... The IndexOutOfBounds exception isn't caught below...
}catch(Exception e){
e.printStackTrace();
Crashlytics.logException(e);
}
}
}
我不确定这些方法是否与错误有关...提前感谢您的任何指示!
[更新] getView():
/**
* Create a new ImageView when requested, filling it with a
* thumbnail or a blank image if no thumb is ready yet.
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
// check if user reached end of list
if(shouldLoadMoreData(position)){
loadMoreData();
}
// can we recycle an old view?
if(convertView == null) {
convertView = mInflater.inflate(R.layout.listing_listview_item, parent, false);
holder = new ViewHolder();
holder.listingTitle = (TextView) convertView.findViewById(R.id.listingTitle);
holder.distancia = (TextView) convertView.findViewById(R.id.distancia);
holder.address = (TextView) convertView.findViewById(R.id.address);
holder.promotions = (TextView) convertView.findViewById(R.id.promotions);
holder.img = (ImageView) convertView.findViewById(R.id.img);
holder.loading = (ProgressBar) convertView.findViewById(R.id.loading);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
JSONObject c = listingsArray.getJSONObject(position);
JSONObject listing = c.getJSONObject("listing");
double distance = c.getDouble("distancia");
String dist = (distance > 1000) ? String.format("%.1fkm", distance/1000) : String.format("%dm", (int)distance);
int p = c.getInt("promotions");
String promotions = (p > 1) ? p+" Ofertas" : p+" Oferta";
holder.listingTitle.setText(listing.getString("title"));
holder.address.setText(listing.getString("address"));
holder.promotions.setText(promotions);
holder.distancia.setText(dist);
} catch (JSONException e) {
e.printStackTrace();
}
// do we have a thumb stored in cache?
imageLoader.DisplayImage(imageURLs[position], holder.img, holder.loading);
return convertView;
}