Android - 适配器中的ListView出错:java.lang.NullPointerException

时间:2013-04-30 15:26:17

标签: android android-listview android-arrayadapter

我在显示列表视图时遇到此错误。问题在于适配器,但我找不到解决方案。这在我的logcat中:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.client.alm.WorkItemAdapter.getView(WorkItemAdapter.java:68)
at android.widget.AbsListView.obtainView(AbsListView.java:2159)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
at android.widget.ListView.onMeasure(ListView.java:1158)
at android.view.View.measure(View.java:15513)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827)

这是我的适配器:

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.client.alm.model.WorkItem;
import com.client.alm.model.WorkItemContainer;

public class WorkItemAdapter extends ArrayAdapter<WorkItem> {

    private WorkItemContainer container;
    private Context context;
    int layoutResourceId;

    public WorkItemAdapter(Context context, int layoutResourceId,
            WorkItemContainer container) {
        super(context, layoutResourceId, container.getWorkitems());
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.container = container;
    }

    static class ViewHolder {
        ImageView imageType;
        TextView type;
        TextView priority;
        ImageView imagePriority;
        TextView key;
        ImageView imageStatus;
        TextView status;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ViewHolder();
            holder.imageType = (ImageView) row
                    .findViewById(R.id.imageTypeWorkitem);
            holder.type = (TextView) row.findViewById(R.id.typeWorkitem);
            holder.imagePriority = (ImageView) row
                    .findViewById(R.id.imagePriority);
            holder.key = (TextView) row.findViewById(R.id.key);
            holder.imageStatus = (ImageView) row.findViewById(R.id.imageStatus);
            holder.status = (TextView) row.findViewById(R.id.status);

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

        WorkItem workitem = container.getWorkitems().get(position);
        holder.type.setText(workitem.getType());
        // holder.imageType.setImageResource(workitem.getIcon());
        holder.priority.setText(workitem.getPriority());
        holder.key.setText(workitem.getKey());
        holder.status.setText(workitem.getStatus());

        return row;
    }

}

以及我如何在我的活动中调用适配器:

WorkItemContainer container = gson.fromJson(resultat,
                    WorkItemContainer.class);

            /**
             * Updating parsed JSON data into ListView
             * */
            WorkItemAdapter adaptateur = new WorkItemAdapter(
                    WorkItemActivity.this, R.layout.workitemitem, container);

            ListView listViewWorkitem = (ListView) findViewById(R.id.list);

            listViewWorkitem.setAdapter(adaptateur);
            super.onPostExecute(resultat);

非常感谢你的帮助。提前谢谢。

1 个答案:

答案 0 :(得分:6)

那是因为当你夸大观点时:

if (row == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ViewHolder();
        holder.imageType = (ImageView) row
                .findViewById(R.id.imageTypeWorkitem);
        holder.type = (TextView) row.findViewById(R.id.typeWorkitem);
        holder.imagePriority = (ImageView) row
                .findViewById(R.id.imagePriority);
        holder.key = (TextView) row.findViewById(R.id.key);
        holder.imageStatus = (ImageView) row.findViewById(R.id.imageStatus);
        holder.status = (TextView) row.findViewById(R.id.status);

        row.setTag(holder);

您没有初始化优先级字段。