[英]Why the NullPointerException happened when call notifyDataSetChanged?
I am trying to update the ListView when the Data is update in the Android. 我正在尝试在Android中更新数据时更新ListView。
I use mLeDeviceListAdapter.addRSSIandAddress("20");
我使用mLeDeviceListAdapter.addRSSIandAddress("20");
to set the value to addRSSIandAddress
function. 将值设置为addRSSIandAddress
函数。
And in the addRSSIandAddress
, it receive the value and call mLeDeviceListAdapter.notifyDataSetChanged();
然后在addRSSIandAddress
,它接收值并调用mLeDeviceListAdapter.notifyDataSetChanged();
. 。 But the NullPointerException
happened at the viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
但是NullPointerException
发生在viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
It seems the viewHolder.deviceRssi
is null or the viewHolder
is null . 看来viewHolder.deviceRssi
为null或viewHolder
为null 。
The code of ListView
and the Adapter
is like the following: ListView
和Adapter
的代码如下:
private LeDeviceListAdapter mLeDeviceListAdapter;
private static ListView device_list;
mLeDeviceListAdapter = new LeDeviceListAdapter(getActivity());
device_list.setAdapter(mLeDeviceListAdapter);
public class LeDeviceListAdapter extends BaseAdapter{
private LayoutInflater mInflator;
ArrayList<BluetoothDevice> mLeDevices;
private String mrssi;
public LeDeviceListAdapter(Context context) {
// TODO Auto-generated constructor stub
mInflator = LayoutInflater.from(context);
}
private void addRSSIandAddress(String rssi) {
// TODO Auto-generated method stub
mrssi = rssi;
mLeDeviceListAdapter.notifyDataSetChanged();
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
Log.d(TAG, "getView");
if(view == null){
view = mInflator.inflate(R.layout.device_list, null);
viewHolder = new ViewHolder();
viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
}else {
viewHolder = (ViewHolder) view.getTag();
viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
}
return view;
}
}
static class ViewHolder {
TextView deviceRssi;
}
How to solve this problem ? 如何解决这个问题呢 ? Thanks in advance. 提前致谢。
As the others have said. 正如其他人所说。 I think you forgot to set 我想你忘记了
view.setTag(viewHolder).
Try Put this line here In your code 尝试将这行放在您的代码中
if(view == null)
{
view = mInflator.inflate(R.layout.device_list, null);
viewHolder = new ViewHolder();
viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
view.setTag(viewHolder); //THE CODE
}
else
{
viewHolder = (ViewHolder) view.getTag();
viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
}
return view;
You use: 你用:
viewHolder = (ViewHolder) view.getTag();
But do you setTag
somewhere? 但是,您是否在某个地方设置了setTag
? I don't think so. 我不这么认为。
Hope it helps 希望能帮助到你
Try with below code: 尝试以下代码:
if(view == null){
view = mInflator.inflate(R.layout.device_list, null);
viewHolder = new ViewHolder();
viewHolder.deviceRssi = (TextView) view.findViewById(R.id.device_rssi);
view.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.deviceRssi.setText("RSSI:" + String.valueOf(mrssi));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.