自定义ListView适配器空对象引用

时间:2017-03-15 18:56:52

标签: java android xml android-layout listview

不确定这里没有初始化的内容,我猜测我必须在列表本身上手动创建一个新行,但我不知道如何开始这样做。这是我用来创建和调用适配器的代码:

/**
 * Retrieve adapter UI
 *
 * @param position current position with the list
 * @param convertView view
 * @param parent container
 * @return view, UI elements
 */
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {

    final ViewHolder handler = new ViewHolder();

    //start
    View row = convertView;
    if(row == null) {
        LayoutInflater inflator = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflator.inflate(R.layout.fragment_delivery_collect_item, parent, false);

    }
    row.setTag(handler);


    final  Order.Item itemRow = (Order.Item) super.getItem(position);
    if(itemRow != null) {
        handler.mName = (TextView) convertView.findViewById(R.id.item_name);
        handler.mSize = (TextView) convertView.findViewById(R.id.item_size);
        handler.mQty = (TextView) convertView.findViewById(R.id.item_qty);
        handler.mPrice = (TextView) convertView.findViewById(R.id.item_price);

        handler.mName.setText(itemList.get(position).getName());
        if (itemList.get(position).getSize() != null) {
            handler.mSize.setText(itemList.get(position).getSize());
        }
        handler.mQty.setText(itemList.get(position).getQty());
        handler.mPrice.setText(String.format("£%s", itemList.get(position).getPrice()));
    }
    return row;
}

public Boolean cleanAll(){
    try {
        itemList.clear();
        notifyDataSetChanged();
        return true;
    }catch (Exception ex){
        Log.i("cleanAll",ex.getMessage());
        return false;
    }
}

class ViewHolder {
    View mView;
    TextView mName, mSize, mQty, mPrice;
}

我尝试使用" Order.Item,"中的信息填充页面上的4个TextView。那个班很简单,但是如果你需要我表明这一点也只是问。

以下是我收到的错误消息:

FATAL EXCEPTION: main
Process: uk.ac.plymouth.www.deliveryapplication, PID: 21370
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at uk.ac.plymouth.www.deliveryapplication.adapters.CollectListViewAdapter.getView(CollectListViewAdapter.java:96)

这是我在片段中创建适配器的地方:

ListView returnedItem = (ListView) rootView.findViewById(R.id.list);
    CollectListViewAdapter adapter = new CollectListViewAdapter(getActivity().getApplicationContext(), R.layout.fragment_delivery_collect_item, true, this);
    for (Order.Item i : currOrder.getITEMS()) {
        adapter.add(i);
    }
    returnedItem.setAdapter(adapter);

1 个答案:

答案 0 :(得分:1)

您的convertView为空。您没有在convertView

中指定布局视图

尝试在使用时使用该行:

row = inflator.inflate(R.layout.fragment_delivery_collect_item, parent, false);

所以:

if(row!= null) {
    handler.mName = (TextView) row.findViewById(R.id.item_name);
    handler.mSize = (TextView) row.findViewById(R.id.item_size);
    handler.mQty = (TextView) row.findViewById(R.id.item_qty);
    handler.mPrice = (TextView) row.findViewById(R.id.item_price);