public class InventoryRFID implements RfidEventsListener { @Override public void eventReadNotify(RfidReadEvents rfidReadEvents) { TagData[] tags = reader.Actions.getReadTags(1000); if (tags == null) { return; } for (final TagData tag : tags) { startBeepingTimer(); if (epcList.add(tag.getTagID())) { runOnUiThread(new Runnable() { @Override public void run() { String epc = tag.getTagID(); if (epc != null && epc.startsWith("40")) { String hexEpc = hexToAscii(epc); Log.e("tag", tag.getTagID()); reader(hexEpc); } } }); } } } public void reader(String epc) { String upc = epc; //UpcUtils.getUPC(epc); Stock s; String name = ""; String abbreviation = ""; String NickName = epc.substring(2, 4); String realName = getRealName(NickName); s = new Stock(epc, epc, realName, realName); s.setTime(FileUtil.getTimes()); s.setAbbreviation(abbreviation); adapter.update(s); btn_update.setText(String.valueOf(adapter.getCount())); }Attempt to invoke virtual method 'void com.ives.upcrfid_zebra.StockInListViewAdapter.update(com.ives.upcrfid_zebra.entiry.Stock)' on a null object reference怎么修改
时间: 2023-12-11 18:06:08 浏览: 174
AndroidStudio中重载方法@Override的使用详解
这个错误通常是因为 `adapter` 为空引起的。因此,你需要检查 `adapter` 是否已经被初始化并且不为空。如果 `adapter` 是 null,那么你需要在使用它之前初始化它。例如:
```
private StockInListViewAdapter adapter;
// 初始化 adapter
adapter = new StockInListViewAdapter(this, R.layout.stock_item, new ArrayList<Stock>());
listView.setAdapter(adapter);
public void reader(String epc) {
// ...
if (adapter != null) {
adapter.update(s);
btn_update.setText(String.valueOf(adapter.getCount()));
}
// ...
}
```
阅读全文