ArrayAdapter中ArrayList上的NullPointerException

时间:2015-02-24 02:46:44

标签: android nullpointerexception android-arrayadapter

我有一个数组适配器,用于在短信应用中设置消息。在发送或接收一条消息时,该应用程序正常工作。但是,一旦发送或接收到第二条消息,应用程序崩溃并显示NullPointerException错误:

java.lang.NullPointerException
            at com.example.feastapp.ChatBoxUi.DiscussArrayAdapter.getView(DiscussArrayAdapter.java:114)
            at android.widget.AbsListView.obtainView(AbsListView.java:2255)
            at android.widget.ListView.makeAndAddView(ListView.java:1790)
            at android.widget.ListView.fillUp(ListView.java:725)
            at android.widget.ListView.layoutChildren(ListView.java:1611)
            at android.widget.AbsListView.onLayout(AbsListView.java:2087)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)

在线:holder.countryName.setText(countries.get(position).comment);

使用inialization将新注释添加到适配器:

adapter.add(new OneComment(false, "from: person" + ": " + message.toString(), "0", "0"));

如何重新配置​​以便不会发生NullPointer?

ArrayAdapter类:

public class DiscussArrayAdapter extends ArrayAdapter<OneComment> {

    class ViewHolder {
        TextView countryName;
        ImageView sharedSpecial;
        LinearLayout wrapper;
    }

    private TextView countryName;
    private ImageView sharedSpecial;


    private List<OneComment> countries = new ArrayList<OneComment>();
    private LinearLayout wrapper;

    private JSONObject resultObject;
    private JSONObject imageObject;

    String getSharedSpecialURL = null;
    String getSharedSpecialWithLocationURL = null;

    String specialsActionURL = "http://" + Global.getIpAddress()
            + ":3000/getSharedSpecial/";

    String specialsLocationActionURL = "http://" + Global.getIpAddress()
            + ":3000/getSharedSpecialWithLocation/";

    String JSON = ".json";

    @Override
    public void add(OneComment object) {
        countries.add(object);
        super.add(object);
    }

    public DiscussArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public int getCount() {
        return this.countries.size();
    }

    private OneComment comment = null;

    public OneComment getItem(int index) {
        return this.countries.get(index);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.message_list_item, parent, false);

            holder = new ViewHolder();
            holder.wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
            holder.countryName = (TextView) row.findViewById(R.id.comment);
            holder.sharedSpecial = (ImageView) row.findViewById(R.id.sharedSpecial);

        } else {
            holder = (ViewHolder) row.getTag();
        }

        Log.v("COMMENTING","Comment is " + countries.get(position).comment);

        //OneComment comment = getItem(position);
        holder.countryName.setText(countries.get(position).comment);

        // Initiating Volley
        final RequestQueue requestQueue = VolleySingleton.getsInstance().getRequestQueue();

        // Check if message has campaign or campaign/location attached
        if (countries.get(position).campaign_id == "0" && countries.get(position).location_id == "0") {

            holder.sharedSpecial.setImageResource(0);

            Log.v("TESTING", "It is working");

        } else if (countries.get(position).campaign_id != "0" && countries.get(position).location_id != "0") {

            // If both were shared
            getSharedSpecialWithLocationURL = specialsLocationActionURL + countries.get(position).campaign_id + "/" + countries.get(position).location_id + JSON;

            // GET JSON data and parse
            JsonObjectRequest getCampaignLocationData = new JsonObjectRequest(Request.Method.GET, getSharedSpecialWithLocationURL, null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {

                            // Parse the JSON:
                            try {
                                resultObject = response.getJSONObject("shared");

                                imageObject = resultObject.getJSONObject("image");
                                adImageURL = imageObject.getString("url");



                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            // Get and set image
                            Picasso.with(getContext()).load("http://" + Global.getIpAddress() + ":3000" + adImageURL).into(holder.sharedSpecial);


                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d("Error.Response", error.toString());
                        }
                    }
            );

            requestQueue.add(getCampaignLocationData);

        } else if (countries.get(position).campaign_id != "0" && countries.get(position).location_id == "0") {

            // Just the campaign is shared
            getSharedSpecialURL = specialsActionURL + countries.get(position).campaign_id + JSON;



            JsonObjectRequest getCampaignData = new JsonObjectRequest(Request.Method.GET, getSharedSpecialURL, null,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {

                            // Parse the JSON:
                            try {
                                resultObject = response.getJSONObject("special_shared");
                                adId = resultObject.getString("id");
                                adCaption = resultObject.getString("ad_caption");
                                adDetail = resultObject.getString("ad_details");
                                adRestaurant = resultObject.getString("restaurant_name");

                                imageObject = resultObject.getJSONObject("image");
                                adImageURL = imageObject.getString("url");

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            // Get and set image
                            Picasso.with(getContext()).load("http://" + Global.getIpAddress() + ":3000" + adImageURL).into(holder.sharedSpecial);

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d("Error.Response", error.toString());
                        }
                    }
            );

            requestQueue.add(getCampaignData);

            // Location set to empty
        }

        // If left is true, then yellow, if not then set to green bubble
        holder.countryName.setBackgroundResource(countries.get(position).left ? R.drawable.bubble_yellow : R.drawable.bubble_green);
        holder.wrapper.setGravity(countries.get(position).left ? Gravity.LEFT : Gravity.RIGHT);

        return row;
    }

}

1 个答案:

答案 0 :(得分:1)

每当您创建新视图时,您都忘记将ViewHolder保存为标记。

if中的第一个getView()语句应如下所示:

    if (row == null) {
        LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.message_list_item, parent, false);

        holder = new ViewHolder();
        holder.wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
        holder.countryName = (TextView) row.findViewById(R.id.comment);
        holder.sharedSpecial = (ImageView) row.findViewById(R.id.sharedSpecial);

        // Store the ViewHolder as a tag.
        row.setTag(holder);

    } else {
        holder = (ViewHolder) row.getTag();
    }

更好的是,您应该使用setTaggetTag的{​​{3}}和setTag(int key, Object tag)变体,以避免与可能在其中使用共享标记的任何其他代码发生冲突自己的方式。您应该使用应用的某个资源ID作为密钥(以确保唯一性) - 例如R.layout.message_list_item