自定义ListView适配器空对象参考 [英] Custom ListView Adapter Null Object Reference
问题描述
不确定在这里还没有初始化什么,我想我必须在列表本身上手动创建一个新行,但是我不知道如何开始.这是我用来创建和调用适配器的代码:
/***检索适配器UI** @param position当前位置与列表* @param convertView视图* @param父容器* @return视图,UI元素*/@Overridepublic View getView(最终int位置,最终View convertView,最终ViewGroup父级){最终的ViewHolder处理程序= new ViewHolder();//开始查看行= 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(位置);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());如果(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()));}返回行}公共布尔cleanAll(){尝试 {itemList.clear();notifyDataSetChanged();返回true;}捕获(异常除外){Log.i("cleanAll",ex.getMessage());返回false;}}类ViewHolder {查看mView;TextView mName,mSize,mQty,mPrice;}
我正在尝试使用"Order.Item"中的信息填充页面上的4个TextView,该类非常简单,但是如果您也需要我展示它,那就问吧.
这是我收到的错误消息:
致命异常:主要流程:uk.ac.plymouth.www.deliveryapplication,PID:21370java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法"android.view.View android.view.View.findViewById(int)"在uk.ac.plymouth.www.deliveryapplication.adapters.CollectListViewAdapter.getView(CollectListViewAdapter.java:96)
这是我在片段中创建适配器的地方:
ListView returnItem =(ListView)rootView.findViewById(R.id.list);CollectListViewAdapter适配器=新的CollectListViewAdapter(getActivity().getApplicationContext(),R.layout.fragment_delivery_collect_item,true,this);对于(Order.Item i:currOrder.getITEMS()){adapter.add(i);}returnItem.setAdapter(adapter);
您的 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);
Not sure what hasn't been initialised here, I'm guessing I have to manually create a new row on the list itself but I have no clue how to even start on that. Here is the code I use to create and call the adapter:
/**
* 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;
}
I'm trying to populate the 4 TextViews on a page with information from "Order.Item," that class Is pretty simple but if you need me to show that too just ask.
Here is the error message I'm getting:
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)
And here is where I create the adapter in the 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);
your convertView
is null. You are not assigning your layout view in convertView
try use the row as you are using:
row = inflator.inflate(R.layout.fragment_delivery_collect_item, parent, false);
so do:
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);
这篇关于自定义ListView适配器空对象参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!