滚动时 ListView 自定义适配器错误
Posted
技术标签:
【中文标题】滚动时 ListView 自定义适配器错误【英文标题】:ListView Custom Adapter error on scrolling 【发布时间】:2020-07-22 20:25:37 【问题描述】:我在listView
的自定义适配器下面附加了我的代码,但是当我滚动列表时,我得到了:
mView 不存在,所以丢弃剩余的点。 java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)' 为空 对象引用 任何人都可以帮助我为什么会收到此错误,这将非常有帮助
public class TransactionAdapter extends BaseAdapter
private static final String TAG = TransactionAdapter.class.getName();
List<Transacation> arrlist;
Context c;
private LayoutInflater inflater;
public TransactionAdapter(Context context, List<Transacation> list)
this.arrlist = list;
this.c = context;
inflater = LayoutInflater.from(this.c);
@Override
public int getCount()
return arrlist.size();
@Override
public Object getItem(int i)
return null;
@Override
public long getItemId(int i)
return 0;
@Override
public View getView(int position, View convertView, ViewGroup parent)
View result;
final MyViewHolder myViewHolder;
myViewHolder = new MyViewHolder();
final Transacation transacation = arrlist.get(position);
if (convertView == null)
convertView = inflater.inflate(R.layout.transaction_adapter, parent,
false);
myViewHolder.tv_date = convertView.findViewById(R.id.tv_date);
myViewHolder.tv_status = convertView.findViewById(R.id.tv_status);
myViewHolder.tv_expiry = convertView.findViewById(R.id.tv_expiry);
myViewHolder.tv_amount = convertView.findViewById(R.id.tv_amount);
result = convertView;
convertView.setTag(myViewHolder);
else
result = convertView;
// myViewHolder.tv_date.setText(""+new java.text.SimpleDateFormat("dd MMM").format(new java.util.Date (transacation.date)));
if(transacation.transactionType.equals("CREDIT"))
myViewHolder.tv_status.setText("Wallet Credited");
myViewHolder.tv_expiry.setText("Expires on " + new java.text.SimpleDateFormat("dd MMM yyyy").format(new java.util.Date (transacation.expiry_date)));
myViewHolder.tv_amount.setText("+₹" + transacation.n_amount);
myViewHolder.tv_amount.setTextColor(Color.parseColor("#23DD47"));
else
myViewHolder.tv_status.setText("Wallet Expired");
myViewHolder.tv_expiry.setVisibility(View.GONE);
myViewHolder.tv_amount.setText("-₹" + transacation.n_amount);
myViewHolder.tv_amount.setTextColor(Color.parseColor("#FB5151"));
return convertView;
private class MyViewHolder
TextView tv_date;
TextView tv_status;
TextView tv_expiry;
TextView tv_amount;
【问题讨论】:
请使用 AutoCompleteTextview 和自定义适配器 请从 logcat 上传准确的错误日志 2020-04-10 10:46:47.022 24571-24571/com.example.zingbus E/ViewRootImpl: mView 不存在,所以丢弃剩余的点。 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”在此之后我的整个列表消失了 参考这篇文章journaldev.com/10416/… 【参考方案1】:当convertView不为null时,你需要通过取回标签来构造ViewHolder,所以将你的getView()
更改为:
@Override
public View getView(int position, View convertView, ViewGroup parent)
View result;
final MyViewHolder myViewHolder;
final Transacation transacation = arrlist.get(position);
if (convertView == null)
convertView = inflater.inflate(R.layout.transaction_adapter, parent,
false);
myViewHolder = new MyViewHolder()
myViewHolder.tv_date = convertView.findViewById(R.id.tv_date);
myViewHolder.tv_status = convertView.findViewById(R.id.tv_status);
myViewHolder.tv_expiry = convertView.findViewById(R.id.tv_expiry);
myViewHolder.tv_amount = convertView.findViewById(R.id.tv_amount);
result = convertView;
convertView.setTag(myViewHolder);
else
result = convertView;
myViewHolder = (MyViewHolder) convertView.getTag();
// myViewHolder.tv_date.setText(""+new java.text.SimpleDateFormat("dd MMM").format(new java.util.Date (transacation.date)));
if(transacation.transactionType.equals("CREDIT"))
myViewHolder.tv_status.setText("Wallet Credited");
myViewHolder.tv_expiry.setText("Expires on " + new java.text.SimpleDateFormat("dd MMM yyyy").format(new java.util.Date (transacation.expiry_date)));
myViewHolder.tv_amount.setText("+₹" + transacation.n_amount);
myViewHolder.tv_amount.setTextColor(Color.parseColor("#23DD47"));
else
myViewHolder.tv_status.setText("Wallet Expired");
myViewHolder.tv_expiry.setVisibility(View.GONE);
myViewHolder.tv_amount.setText("-₹" + transacation.n_amount);
myViewHolder.tv_amount.setTextColor(Color.parseColor("#FB5151"));
return convertView;
【讨论】:
【参考方案2】:在我看来,请检查 findViewById 是否具有正确的 ID 名称,或者如果与此无关,请从
更新myViewHolder.tv_amount.setText("+₹" + transacation.n_amount);
到
myViewHolder.tv_amount.setText("+₹" + String.ValueOf(transacation.n_amount));
或者请发布完整的日志值
【讨论】:
以上是关于滚动时 ListView 自定义适配器错误的主要内容,如果未能解决你的问题,请参考以下文章
在 ListView 的自定义适配器中从 URL 加载图像(Android Studio)
自定义 ListView 适配器。 TextChangedListener 调用错误的 EditText