2014-01-13 45 views
0

我試圖創建圖像的GridView。我在複製this android developers tutorial,但是他們將可繪製硬編碼爲Integer[],而我必須將其設置爲用戶選擇的內容。 mListContents被填充了對象。 path和pathA都是以一個值開始的。這一切都通過調試器得到確認。當它到達mList.add(pathA)時,它會拋出一個nullpointerexception。在調試時,它在ActivityThread中顯示「未找到源」,併爲我提供了「編輯源查找路徑」的選項。任何問題從教程Integer[]更改爲List<Integer>Nullpointerexception添加到列表時「編輯源查找路徑」

public class ImageAdapter extends BaseAdapter { 
    private Context mContext; 
    private int mMenuId; 
    dbhelper db; 

    List<ClothingItem> mListContents; 
    List<Integer> mList; 

    public ImageAdapter(Context c, int menuId) { 
     mListContents = new ArrayList<ObjectGeneric>(); 
     mContext = c; 
     mMenuId = menuId; 
     db = new dbhelper(mContext); 
     setList(mMenuId); 
     setDrawableList(); 
    } 

private void setDrawableList(){ 
      for(ObjectGeneric item : mListContents){ 
       int path = item.getImagePath(); 
       Integer pathA = (Integer) path; 
       mList.add(pathA); 
      } 
     } 

    // create a new ImageView for each item referenced by the Adapter 
     public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView imageView; 
      if (convertView == null) { // if it's not recycled, initialize some attributes 
       imageView = new ImageView(mContext); 
       imageView.setLayoutParams(new GridView.LayoutParams(85, 85)); 
       imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
       imageView.setPadding(8, 8, 8, 8); 
      } else { 
       imageView = (ImageView) convertView; 
      } 

      imageView.setImageResource(mList.get(position)); 
      return imageView; 
     } 

回答

1

從乍一看,它看起來像你沒有初始化mList,所以空指針異常是正確的。嘗試初始化它,如下所示:

public ImageAdapter(Context c, int menuId) { 
    mListContents = new ArrayList<ClothingItem>(); 
    mList = new ArrayList<Integer>(); // <--- here 

    mContext = c; 
    mMenuId = menuId; 
    db = new dbhelper(mContext); 
    setList(mMenuId); 
    setDrawableList(); 
} 
+0

這絕對應該是謝謝你。 mListContents啓動,但mList不是!確認後將標記爲答案。謝謝! – user3164083