2016-04-26 75 views
-3

在我的列表視圖結束時得到這個錯誤。當我滑下我的清單項目顯示「不幸meltwish已停止。java.lang.IndexOutOfBoundsException:索引15無效,大小爲15

我是全新的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

尺寸是15和u嘗試更設定位置超過15通過位置= 2 *位置; –

+0

爲什麼你使用這個位置= 2 *位置; ?? – sushildlh

+0

哪裏是logcat ???????? –

回答

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); 
+0

與你更換我的代碼錯誤之後走了,但我在我的ImageView每個圖像都顯示相同的 怎麼做的就是解決這個問題??? –

+0

什麼,但??新錯誤?? – sushildlh

+0

你會請告訴我爲什麼你使用這一行的位置= 2 *位置; – sushildlh

0

改變你的getCount()

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

} 

而在getView(..)方法除去

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

不工作....它仍然會崩潰:( –

+0

發表您的logcat。 –

相關問題