0

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

解決策を見つけるのを手伝ってください..

前もって感謝します..

4

2 に答える 2

0

NullPointerの原因を知っていますか? ダウンロードを行うのはコード内のビューまたは何かですか?

于 2012-09-26T09:17:05.307 に答える
0

その中にホルダーオブジェクトを設定するために convertView にタグを設定しなかったことの1つ。

編集済み

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 here としてコメントして質問を更新してください

于 2012-09-26T09:47:24.027 に答える