2014-01-13 25 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