我創建了一個AsyncTask並解析了JSON,並在後臺將它放到了List中,並且我的應用程序有錯誤,我無法理解該怎麼辦?這個錯誤當我的應用程序創建! ПарсюJSONикидаюеговЛистсМассивомиприложениекрашитсяAndroid ArrayIndexOutOfBoundException
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)
檢查列表變量的長度,它只是長度爲1. –
請給出英文信息,您將獲得更多人的幫助:) – swiftBoy