Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中从JSON加载BaseAdapter中的图像_Android_Json_Android Imageview_Baseadapter_Custom Lists - Fatal编程技术网

如何在Android中从JSON加载BaseAdapter中的图像

如何在Android中从JSON加载BaseAdapter中的图像,android,json,android-imageview,baseadapter,custom-lists,Android,Json,Android Imageview,Baseadapter,Custom Lists,我已经创建了一个包含TextView、ImageView和Button的自定义列表。现在我试图显示来自JSON url的图像,但当我运行代码时,我收到一个NullPointerException。我不知道为什么会出现这个错误,因为TextView和按钮显示了各自的信息 以下是BaseAdapter的代码: public class SearchAdapter extends BaseAdapter { Context context; LayoutInflater inflat

我已经创建了一个包含TextView、ImageView和Button的自定义列表。现在我试图显示来自JSON url的图像,但当我运行代码时,我收到一个NullPointerException。我不知道为什么会出现这个错误,因为TextView和按钮显示了各自的信息

以下是BaseAdapter的代码:

public class SearchAdapter extends BaseAdapter {

    Context context;
    LayoutInflater inflater;
    View vw;
    ArrayList<String> unameList = new ArrayList<String>();
    ArrayList<String> statusList = new ArrayList<String>();
    ArrayList<String> uidList = new ArrayList<String>();
    ArrayList<String> picList = new ArrayList<String>();
    String id, message, url;
    boolean status;
    ConnectionStatus cs;

    public SearchAdapter(Context context, LayoutInflater inflater, ArrayList<String> list1, ArrayList<String> list2,
            ArrayList<String> list3, ArrayList<String> list4, String id) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.inflater = inflater;
        unameList.clear();
        statusList.clear();
        uidList.clear();
        picList.clear();
        unameList = list1;
        statusList = list2;
        uidList = list3;
        picList = list4;
        Log.v("srch", String.valueOf(picList));
        this.id = id;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return unameList.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    private class ViewHolder {
        ImageView img;
        TextView tv;
        Button btn;
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder holder;
        if(convertView == null)
        {
            convertView = inflater.inflate(R.layout.list_items, null);
            holder = new ViewHolder();
            holder.img = (ImageView)convertView.findViewById(R.id.imageView1);
            holder.tv = (TextView)convertView.findViewById(R.id.textView1);
            holder.btn = (Button)convertView.findViewById(R.id.button1);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
        }
        if(picList.get(position).startsWith("http:"))
        {
            try
            {
                URL url = new URL(picList.get(position));
                Log.v("url", url.toString());
                URLConnection conn = url.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                Bitmap bm = BitmapFactory.decodeStream(bis);
                holder.img.setImageBitmap(bm);
                bis.close();
                is.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
                Log.e("Exception",e.getMessage());
            }
        }
        holder.tv.setText(unameList.get(position));
        if(statusList.get(position).contains("1"))
        {
            holder.btn.setText("listening");
        }
        else
        {
            holder.btn.setText("listen");
        }
        return convertView;
    }
} 
相反,如果
picList.get(position)
您可以尝试以下链接:

http://abhishekpnf.my3gb.com/test/uploads/180px-Lamborghini_Logo.svg.png

请帮我找到解决办法


提前感谢。

您知道是什么原因导致空指针吗?下载的是视图还是代码中的某个内容?

有一件事您没有在convertView中设置标记以在其中设置holder对象

已编辑

final ViewHolder holder;
    if(convertView == null)
    {
        convertView = inflater.inflate(R.layout.list_items, null);
        holder = new ViewHolder();
        holder.img = (ImageView)convertView.findViewById(R.id.imageView1);
        holder.tv = (TextView)convertView.findViewById(R.id.textView1);
        holder.btn = (Button)convertView.findViewById(R.id.button1);

        convertView.setTag(holder); // missing write this statement in if block at the end
    }
    else{
        ........

检查之后,如果您遇到相同的错误,则只需在该行上用注释更新您的问题//error

您可以在此处打印空指针异常吗?我想您还没有尝试我的代码。。我仍然得到相同的空指针错误。。。
final ViewHolder holder;
    if(convertView == null)
    {
        convertView = inflater.inflate(R.layout.list_items, null);
        holder = new ViewHolder();
        holder.img = (ImageView)convertView.findViewById(R.id.imageView1);
        holder.tv = (TextView)convertView.findViewById(R.id.textView1);
        holder.btn = (Button)convertView.findViewById(R.id.button1);

        convertView.setTag(holder); // missing write this statement in if block at the end
    }
    else{
        ........