gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:32:12 25 4
gpt4 key购买 nike

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

/**
* 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)

这里是我在 fragment 中创建适配器的地方:

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);

最佳答案

您的convertView 为空。您没有在 convertView

中分配布局 View

尝试使用您正在使用的行:

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);

关于java - 自定义 ListView 适配器空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42818435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com