在我的应用程序中,我正在扫描蓝牙设备。在阅读时,我得到了Nullpointer异常,下面是代码片段。

    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是“客户”列表适配器的对象。

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 - 读取蓝牙设备时出现Nullpointer异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26483840/

10-10 02:54