添加到列表“编辑源查找路径”时出现Nullpointerexception

时间:2014-01-13 22:55:04

标签: android gridview arraylist

我正在尝试创建图像的gridView。我正在复制this android developers tutorial,但是他们将可绘制的内容硬编码为Integer[],而我必须将我的内容设置为用户选择的内容。 mListContents填充了对象。 path和pathA都是用值启动的。这一切都通过调试器确认。到达mList.add(pathA)时,它会抛出nullpointerexception。调试时,它在ActivityThread中显示“source not found”,并为我提供“编辑源查找路径”选项。是否有任何问题从教程Integer[]更改为List<Integer>

    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 个答案:

答案 0 :(得分: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();
}