0

I create a AsyncTask and parse JSON and in background i put it to List, and my app have errors, i can't understand what to do? This error when my app create!!! Парсю JSON и кидаю его в Лист с Массивом и приложение крашится

 List<Row> result = new ArrayList<Row>();
    for (int i = 0; i < array.length(); i++) {
        try {
        result.add(new Row(array.getJSONObject(i).getString("thumbnail")));
        } catch (JSONException e) {
                    e.printStackTrace();
    }
    }

        return result;
    }

    @Override
    protected void onPostExecute(List<Row> result) {
        adapter.clear();
        adapter.add(result);
    }

Row.java

class Row implements Parcelable{

         public final Creator<Row> CREATOR = new Creator<Row>() {
                @Override
                public Row[] newArray(int size) {
                    return new Row[size];
                }

                @Override
                public Row createFromParcel(Parcel parcel) {
                    return new Row(parcel);
                }
            };


        ArrayList<String> thumbs;

        public Row(String thumbs) {
            this.thumbs = new ArrayList<String>();
            this.thumbs.add(thumbs);
        }

        public Row(Parcel parcel) {
            parcel.readStringList(thumbs);
        }

        @Override
        public int describeContents() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public void writeToParcel(Parcel parcel, int flags) {
            parcel.writeStringList(thumbs);         
        }

    }

Adapter.java

 private final class ImageAdapter extends BaseAdapter {
        private final List<Row> list;
        private ImageLoader loader;
        private DisplayImageOptions options;

        public ImageAdapter() {
            this(new ArrayList<Row>());

           options = new DisplayImageOptions.Builder()
            .cacheInMemory()
            .cacheOnDisc()
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

           loader = ImageLoader.getInstance();
           loader.init(ImageLoaderConfiguration.createDefault(MainActivity.this));
        }

        public void clear() {
            list.clear();
            notifyDataSetChanged();
        }

        public void add(List<Row> rows) {
            list.addAll(rows);
            notifyDataSetChanged();
        }



        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Row getItem(int position) {
            return list.get(position);
        }

        public View getView(int position, View view, ViewGroup container) {
            if (view == null) {
                view = getLayoutInflater().inflate(R.layout.grid_item, container, false);
            }

            ImageView image = (ImageView)view.findViewById(R.id.imageGrid);
            Row row = getItem(position);
            // Bind row on your view here            
            String[] qwe = row.thumbs.toArray(new String[row.thumbs.size()]);
            loader.displayImage(qwe[position], image, options);
            return view;
        }

        public ImageAdapter(List<Row> list) {
            this.list = list;
        }
    }

LogCat

    FATAL EXCEPTION: main
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
    at com.example.tectob6iu.MainActivity$ImageAdapter.getView(MainActivity.java:98)
    at android.widget.AbsListView.obtainView(AbsListView.java:2159)
    at android.widget.GridView.makeAndAddView(GridView.java:1341)
    at android.widget.GridView.makeRow(GridView.java:341)
    at android.widget.GridView.fillDown(GridView.java:283)
    at android.widget.GridView.fillFromTop(GridView.java:417)
    at android.widget.GridView.layoutChildren(GridView.java:1229)
    at android.widget.AbsListView.onLayout(AbsListView.java:1994)
    at android.view.View.layout(View.java:14003)
    at android.view.ViewGroup.layout(ViewGroup.java:4375)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14003)
    at android.view.ViewGroup.layout(ViewGroup.java:4375)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
    at android.view.View.layout(View.java:14003)
    at android.view.ViewGroup.layout(ViewGroup.java:4375)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14003)
    at android.view.ViewGroup.layout(ViewGroup.java:4375)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
    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:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5191)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
    at dalvik.system.NativeStart.main(Native Method)
2
  • 1
    Check the length of the list variable , It is only of length 1. Commented Dec 18, 2012 at 8:58
  • please give information in English, you will get more help for more people :) Commented Dec 18, 2012 at 9:13

1 Answer 1

2
      String[] qwe = row.thumbs.toArray(new String[row.thumbs.size()]);
      loader.displayImage(qwe[position], image, options);

are you sure qwe contains at least postion + 1 elements ?

Sign up to request clarification or add additional context in comments.

1 Comment

I change my code success, and now new problem when i click refresh he load new Json and add to this List and LogCat give a error java.lang.IndexOutOfBoundsException: Invalid index 23, size is 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.