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

时间:2012-09-26 09:05:07

标签: android json android-imageview baseadapter custom-lists

我创建了一个包含TextView,ImageView和Button的自定义列表。现在我试图从JSON URL显示图像,但是当我运行我的代码时,我收到一个NullPointerException。我不知道为什么我收到此错误,因为TextView和Button正在显示各自的信息。

以下是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;
    }
} 

logcat的:

09-26 14:13:34.294: E/AndroidRuntime(1376): FATAL EXCEPTION: main
09-26 14:13:34.294: E/AndroidRuntime(1376): java.lang.NullPointerException
09-26 14:13:34.294: E/AndroidRuntime(1376):     at com.pnf.flutterr.SearchAdapter.getView(SearchAdapter.java:105)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.widget.ListView.fillDown(ListView.java:652)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.widget.ListView.fillFromTop(ListView.java:709)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.widget.ListView.layoutChildren(ListView.java:1580)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.view.View.layout(View.java:7035)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.view.View.layout(View.java:7035)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.view.View.layout(View.java:7035)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.view.View.layout(View.java:7035)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.view.View.layout(View.java:7035)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.os.Looper.loop(Looper.java:123)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at java.lang.reflect.Method.invoke(Method.java:521)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-26 14:13:34.294: E/AndroidRuntime(1376):     at dalvik.system.NativeStart.main(Native Method)

相反,如果picList.get(position)您可以尝试此链接:

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

请帮助我找到解决方案..

先谢谢..

2 个答案:

答案 0 :(得分:0)

你知道什么导致NullPointer?是代码中的视图还是其他东西在进行下载?

答案 1 :(得分:0)

有一件事你没有在convertView中设置标签来设置持有者对象。

<强>被修改

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{
        ........

如果你得到同样的错误,请在此之后检查,然后在该行注释为//错误更新你的问题