gpt4 book ai didi

java - 读取蓝牙设备时出现空指针异常

转载 作者:行者123 更新时间:2023-12-01 12:24:03 27 4
gpt4 key购买 nike

在我的应用程序中,我正在扫描蓝牙设备。在阅读时,我收到 Nullpointer Exception,下面是代码 fragment 。

    private  BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
// TODO Auto-generated method stub

getActivity().runOnUiThread(new Runnable() {

@Override
public void run() {
// TODO Auto-generated method stub

1. mLeDeviceListAdapter.addDevices(device);
2. mLeDeviceListAdapter.notifyDataSetChanged();

}
});
}
}

我在上面的第 1 行中收到 NullpointerException。这里 mLeDeviceListAdapter 是 Custome 列表适配器的对象。

public class LeDeviceListAdapter extends BaseAdapter{

private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
Bundle savedInstanceState;

public LeDeviceListAdapter() {
// TODO Auto-generated constructor stub
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mInflator = AvailableDevices.this.getLayoutInflater(savedInstanceState);

}

public void addDevices(BluetoothDevice device){

if(!mLeDevices.contains(device)){
mLeDevices.add(device);

}

}

在 OnActivityCreated 方法中,我调用所有上述函数,如下所示。

public class AvailableDevices extends ListFragment {

public void onActivityCreated(Bundle savedInstanceState) {

LeDeviceListAdapter dListAdapter = new LeDeviceListAdapter();

setListAdapter(dListAdapter);
scanLeDevice(true);
}

scanLeDevice函数将调用mLeScanCallback方法。

我缺少获得 NullpointerException 的地方..

谢谢

最佳答案

初始化您的mLeDeviceListAdapter

mLeDeviceListAdapter=new LeDeviceListAdapter();

关于java - 读取蓝牙设备时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26483840/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com