使用php从mysql数据库中获取图像并在android

时间:2015-11-26 12:18:39

标签: android gridview

我尝试使用php从mysql数据库中获取图像路径并在gridview中显示图像。

我的问题是在设置图片时在适配器中出现了NPE(NullPointerException)。

这是我的GridViewAdapter

public class GridViewAdapter extends BaseAdapter {

    private Context context;
    public ArrayList<HashMap<String,String>> mThumbIds = new ArrayList<HashMap<String, String>>();
    AQuery aQuery;
    private LayoutInflater mInflater;

    public GridViewAdapter(Context context,ArrayList<HashMap<String,String>> data) {
        this.context= context;
        this.mThumbIds = data;
        this.aQuery = new AQuery(context);
    }

    @Override
    public int getCount() {
        return mThumbIds.size();
    }

    @Override
    public Object getItem(int position) {
        return  0;
    }


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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.photo_gallery_list_item, null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        aQuery.id(holder.imageView).image(mThumbIds.get(position).get("flag"), true, true);
        return convertView;
    }

    class ViewHolder {

        ImageView imageView;

    }
}

启动NPE的线路如下:

 convertView = mInflater.inflate(R.layout.photo_gallery_list_item, null);

有人可以建议哪里错了吗?

3 个答案:

答案 0 :(得分:0)

Please initialize your mInflater like below.
 mInflater = LayoutInflater.from(context);

答案 1 :(得分:0)

mInflater为空。你从未分配过它。

GridViewAdapter

中传入一个或与之类似
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

答案 2 :(得分:0)

只是因为你没有初始化你的mInflater;

在构造函数中添加此行;

mInflater = LayoutInflater.from(context);