Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java IndexOutOfBoundsException:索引0无效,大小为0_Java_Android_Indexoutofboundsexception - Fatal编程技术网

Java IndexOutOfBoundsException:索引0无效,大小为0

Java IndexOutOfBoundsException:索引0无效,大小为0,java,android,indexoutofboundsexception,Java,Android,Indexoutofboundsexception,我的代码在某个时候抛出了一个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.

我的代码在某个时候抛出了一个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请求以从服务器获取数据数组 -我使用这个数组来填充一个列表视图,其中包含用户可以选择的几个选项

经过几次测试后,我注意到只有当我从服务器获得的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();
}
我不确定这些方法是否与错误有关。。。提前感谢您的指导

[更新]getView():


你试过一步一步地调试它吗?在哪里会弹出错误?您能否显示
处理程序
类中
.post()
方法的实现?@NitroNbg我这样做了,并运行每一步,直到到达Handler.post(bd);陈述这是在工作线程中运行的,所以我尝试了sleep(),在线程唤醒之前抛出异常up@Math处理程序的实例化如下:
handler handler=new handler()//在UI线程中显示图像的处理程序
。当我转到handler.post声明时,它会将我带到android.os中handler.java类的
post(Runnable r)
方法package@CSmith我正在用覆盖的getView()更新我的问题。无论如何,按照Ajai的建议,我从适配器中删除了viewFooter,异常消失了。。。当getView的位置与viewFooter索引匹配时,可能发生了什么事情?
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);
        }
    }
}
/**
 * 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;
}