问题
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);
回答1:
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);
来源:https://stackoverflow.com/questions/42818435/custom-listview-adapter-null-object-reference