读取蓝牙设备时出现Nullpointer异常

时间:2014-10-21 09:55:04

标签: java android bluetooth

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

    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 ..

由于

1 个答案:

答案 0 :(得分:1)

初始化您的mLeDeviceListAdapter

mLeDeviceListAdapter=new LeDeviceListAdapter();