自定义列表视图适配器大小为null

时间:2016-08-11 17:49:59

标签: android json listview

目前我正在尝试使用JSON

填充自定义列表视图

它可以很好地处理虚拟数据,我的json请求很好。

所有这些代码都在onCreate视图中,我知道我们应该使用Async任务或某种类型来生成一个新线程来提取数据,但我想把它放在这里用于测试目的所以我知道我可以提取数据第一。

OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new BasicAuthInterceptor()).addNetworkInterceptor(new StethoInterceptor()).build();

    String url = "http://10.0.2.2:3000/response";

    Request request = new Request.Builder().url(url)
            .build();


    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            Log.i(TAG, "call api error");
            Log.e("Volley", e.toString());
        }

        @Override
        public void onResponse(Call call, okhttp3.Response response)  {

            try {
                if (!response.isSuccessful())
                    throw new IOException("Unexpected code " + response);
                final String body = response.body().string();

                JSONObject Jobject = new JSONObject(body);


                JSONArray Jarray = Jobject.getJSONArray("inboxMsgList");

                Log.e("Array", String.valueOf(Jarray.length()));
//Initializing my ArrayLis here I already defined it where I have my other variables defined.
                    arrMessageData = new ArrayList<>();

                JSONObject object = Jarray.getJSONObject(0);

                MessageData msg3 = new MessageData();
                msg3.setName((String) object.get("fromUser"));
                msg3.setMessage((String) object.get("message"));
                msg3.setImage(R.drawable.msgone);
                msg3.setRead(true);
                msg3.setSubject((String) object.get("subject"));

                arrMessageData.add(msg3);

                Log.e("DetailObject", msg3.getName());


                // Log.e("FullJsonReply", body.toString());
            } catch (Exception e){
                Log.e("Volley2", e.toString());
            }

        }

    });


   adapter = new CustomAdapterReferral(getActivity().getBaseContext(), arrMessageData);
    usersListView.setAdapter(adapter);

我得到的错误是:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int java.util.ArrayList.size()'                                                                                在com.adilpatel.vitalengine.ListAdapters.CustomAdapterReferral.getCount(CustomAdapterReferral.java:40)

第40行是我的适配器中的getCount

public int getCount() {
    return arrMessageData.size();
}

我在它返回大小

的位置放置了一个断点

enter image description here

看起来大小为1但它仍然提供空指针异常。是因为适配器是空的吗?什么是适配器?

编辑:这是我的CustomAdapterReferral:

public class CustomAdapterReferral extends BaseAdapter {

    ArrayList<MessageData> arrMessageData;
    Context context;
    private static LayoutInflater inflater = null;

    public CustomAdapterReferral(Context mainActivity, ArrayList<MessageData> arrMessageData){


        this.arrMessageData = arrMessageData;

        context = mainActivity;


        inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }



    public int getCount() {
        return arrMessageData.size();
    }

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

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

    public class ViewHolder {
        TextView referralName;
        ImageView referralImage;
        TextView referralMessage;


    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=new ViewHolder();

        View rootView = inflater.inflate(R.layout.list_item_referral, null);


        holder.referralName = (TextView) rootView.findViewById(R.id.referralName);
        holder.referralImage =(ImageView) rootView.findViewById(R.id.referralImage);
        holder.referralMessage= (TextView) rootView.findViewById(R.id.referralMessage);

        holder.referralMessage.setText("" + arrMessageData.get(position).getMessage());
        holder.referralName.setText("" + arrMessageData.get(position).getName());
        holder.referralImage.setImageResource(arrMessageData.get(position).getImage());

        return rootView;
    }
}

1 个答案:

答案 0 :(得分:1)

使用处理程序

 private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    adapter = new CustomAdapterReferral(getActivity().getBaseContext(), arrMessageData);
                    usersListView.setAdapter(adapter);
                    break;
                default:
                    Log.d("TAG", msg.what + " ? ");
                    break;
            }
        }
    };

并在OkHttpClient中使用此

 @Override
        public void onResponse(Call call, okhttp3.Response response)  {
  //YOUR CODE
 //END
handler.sendEmptyMessage(1);

}