java.lang.IndexOutOfBoundsException:索引15无效,大小为15

时间:2016-04-26 10:24:13

标签: android listview layout

在我的listview末尾出现此错误。当我向下滑动我的列表项时,它的显示“不幸的是,融化已停止。

我是android和编码的新手,所以我无法弄清楚究竟是什么问题。可能是一个小问题,但对我来说这很刺激

这是我的代码

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = MyApplication.getInstance().getImageLoader();

public CustomListAdapter(Activity activity, List<Movie> movieItems) {
    this.activity = activity;
    this.movieItems = movieItems;
}

@Override
public int getCount() {
    int size = movieItems.size()/2;
    return size ==0 ? size : size +1;// +1 for odd number of items like 3 or 5 or 7 ..so on.
}

@Override
public Object getItem(int location) {
    return movieItems.get(location);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //And also you have handle the odd Items case.Better add extra null at the end if you have odd number of items

    if(position != 0) {
        position = 2 * position;
    }

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);

    if (imageLoader == null)
        imageLoader = MyApplication.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);
   NetworkImageView thumbNail2 = (NetworkImageView) convertView
           .findViewById(R.id.thumbnail2);

    // getting movie data for the row

    Movie m1 = movieItems.get(position);
    Movie m2 = movieItems.get(position+1);
    thumbNail.setImageUrl(m1.getThumbnailUrl(), imageLoader);
    thumbNail2.setImageUrl(m2.getThumbnailUrl(), imageLoader);

    //thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
    //thumbNail2.setImageUrl(m.getThumbnailUrl(), imageLoader);

    return convertView;
}

}

logcat

 04-26 16:07:22.638 17882-17882/? W/dalvikvm: emulated external storage as primary storage, no need to bind temp storage to legacy
 04-26 16:07:22.638 17882-17882/? W/dalvikvm: emulated external storage as primary storage, no need to bind temp storage to legacy
 04-26 16:07:22.648 17882-17882/? D/dalvikvm: Late-enabling CheckJNI
 04-26 16:07:22.788 17882-17882/com.example.aninesoft.meltwish E/dalvikvm: heapsize Dalvik_dalvik_system_VMRuntime_nativeMinimumHeapSize 0
 04-26 16:07:22.788 17882-17882/com.example.aninesoft.meltwish E/dalvikvm: heapsize Dalvik_dalvik_system_VMRuntime_nativeMinimumHeapSize 13281984
 04-26 16:07:22.818 17882-17882/com.example.aninesoft.meltwish W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
 04-26 16:07:22.818 17882-17882/com.example.aninesoft.meltwish I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
 04-26 16:07:22.818 17882-17882/com.example.aninesoft.meltwish W/dalvikvm: VFY: unable to resolve interface method 17417: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
 04-26 16:07:22.818 17882-17882/com.example.aninesoft.meltwish D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
 04-26 16:07:22.818 17882-17882/com.example.aninesoft.meltwish I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
 04-26 16:07:22.818 17882-17882/com.example.aninesoft.meltwish W/dalvikvm: VFY: unable to resolve interface method 17421: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
 04-26 16:07:22.818 17882-17882/com.example.aninesoft.meltwish D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
 04-26 16:07:22.858 17882-17882/com.example.aninesoft.meltwish D/ViewConfiguration: init scroll_friction: 0.017
 04-26 16:07:22.918 17882-17882/com.example.aninesoft.meltwish I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
 04-26 16:07:22.918 17882-17882/com.example.aninesoft.meltwish W/dalvikvm: VFY: unable to resolve virtual method 429: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
 04-26 16:07:22.918 17882-17882/com.example.aninesoft.meltwish D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
 04-26 16:07:22.918 17882-17882/com.example.aninesoft.meltwish I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
 04-26 16:07:22.918 17882-17882/com.example.aninesoft.meltwish W/dalvikvm: VFY: unable to resolve virtual method 451: Landroid/content/res/TypedArray;.getType (I)I
 04-26 16:07:22.918 17882-17882/com.example.aninesoft.meltwish D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
 04-26 16:07:23.168 17882-17882/com.example.aninesoft.meltwish D/AbsListView: reset listView argument, mVelocityScale: 1.0 mMinimumVelocity: 75 mTouchSlop: 12 mDensityScale: 1.5
 04-26 16:07:23.338 17882-17882/com.example.aninesoft.meltwish D/OpenGLRenderer: Enabling debug mode 0
 04-26 16:07:25.050 17882-17882/com.example.aninesoft.meltwish D/MainActivity: [{"genre":["Action","Drama","Sci-Fi"],"image":"http:\/\/api.androidhive.info\/json\/movies\/1.jpg","title":"Dawn of the Planet of the Apes","rating":8.3,"releaseYear":2014},{"genre":["Action","Sci-Fi","Thriller"],"image":"http:\/\/api.androidhive.info\/json\/movies\/2.jpg","title":"District 9","rating":8,"releaseYear":2009},{"genre":["Action","Adventure","Sci-Fi"],"image":"http:\/\/api.androidhive.info\/json\/movies\/3.jpg","title":"Transformers: Age of Extinction","rating":6.3,"releaseYear":2014},{"genre":["Action","Sci-Fi","Thriller"],"image":"http:\/\/api.androidhive.info\/json\/movies\/4.jpg","title":"X-Men: Days of Future Past","rating":8.4,"releaseYear":2014},{"genre":["Drama","Thriller"],"image":"http:\/\/api.androidhive.info\/json\/movies\/5.jpg","title":"The Machinist","rating":7.8,"releaseYear":2004},{"genre":["Action","Drama","History"],"image":"http:\/\/api.androidhive.info\/json\/movies\/6.jpg","title":"The Last Samurai","rating":7.7,"releaseYear":2003},{"genre":["Action","Adventure","Fantasy"],"image":"http:\/\/api.androidhive.info\/json\/movies\/7.jpg","title":"The Amazing Spider-Man 2","rating":7.3,"releaseYear":2014},{"genre":["Action","Drama","Sci-Fi"],"image":"http:\/\/api.androidhive.info\/json\/movies\/8.jpg","title":"Tangled","rating":7.9,"releaseYear":2010},{"genre":["Animation","Comedy","Family"],"image":"http:\/\/api.androidhive.info\/json\/movies\/9.jpg","title":"Rush","rating":8.3,"releaseYear":2013},{"genre":["Horror","Thriller"],"image":"http:\/\/api.androidhive.info\/json\/movies\/10.jpg","title":"Drag Me to Hell","rating":6.7,"releaseYear":2009},{"genre":["Animation","Comedy","Family"],"image":"http:\/\/api.androidhive.info\/json\/movies\/11.jpg","title":"Despicable Me 2","rating":7.6,"releaseYear":2013},{"genre":["Action","Crime"],"image":"http:\/\/api.androidhive.info\/json\/movies\/12.jpg","title":"Kill Bill: Vol. 1","rating":8.2,"releaseYear":2003},{"genre":["Animation","Adventure","Comedy"],"image":"http:\/\/api.androidhive.info\/json\/movies\/13.jpg","title":"A Bug's Life","rating":7.2,"releaseYear":1998},{"genre":["Comedy"],"image":"http:\/\/api.androidhive.info\/json\/movies\/14.jpg","title":"Life of Brian","rating":8.9,"releaseYear":1972},{"genre":["Animation","Adventure","Family"],"image":"http:\/\/api.androidhive.info\/json\/movies\/15.jpg","title":"How to Train Your Dragon","rating":8.2,"releaseYear":2010}]
 04-26 16:07:25.511 17882-17966/com.example.aninesoft.meltwish D/Bitmap: recycle bitmap...1609527056
 04-26 16:07:25.881 17882-17964/com.example.aninesoft.meltwish D/Bitmap: recycle bitmap...1612138624
                                                                    << 
 04-26 16:07:27.382 17882-17963/com.example.aninesoft.meltwish D/Bitmap: recycle bitmap...1614207312

2 个答案:

答案 0 :(得分:0)

更改您的getCount()

@Override
public int getCount() {
// TODO Auto-generated method stub
return movieItem.size();

}

并在getView(..)方法中删除

if(position != 0) {
    position = 2 * position;
}

答案 1 :(得分:0)

if(position != 0) {
 Movie m1 = movieItems.get(position-1);
        Movie m2 = movieItems.get(position);
}

而不是

Movie m1 = movieItems.get(position-1);
            Movie m2 = movieItems.get(position);