ListView btn_updatepublic 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); //soundPoolHelper.play("happy1",false); adapter.update(s); btn_update.setText(String.valueOf(adapter.getCount())); }中的btn_update.setText为什么无效
时间: 2023-12-10 17:39:08 浏览: 92
可能是因为您没有在UI线程上更新UI。在Android中,只有UI线程才能更新UI元素。您可以尝试将更新UI元素的代码包装在runOnUiThread方法中,或使用Handler来更新UI元素。例如:
```
runOnUiThread(new Runnable() {
@Override
public void run() {
btn_update.setText(String.valueOf(adapter.getCount()));
}
});
```
或者:
```
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
btn_update.setText(String.valueOf(adapter.getCount()));
}
});
```
相关问题
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怎么修改
这个错误通常是因为 `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()));
}
// ...
}
```
阅读全文