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

标签 java android bluetooth

在我的应用程序中,我正在扫描蓝牙设备。在阅读时,我收到 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/

相关文章:

java - 为什么我无法使用 Google Classroom API?

android - 以编程方式将我的应用程序添加到android中的AutoStart应用程序列表

ios - 如何将unix时间戳转换为NSData对象?

android - 蓝牙:不使用 UUID 连接

用于启用蓝牙并连接到特定蓝牙设备的 Windows 10 批处理脚本

java - 使用滚动事件放大和缩小

java - 错误 : The package org. apache.commons 无法访问

java - 如果 session 为真,则将用户信息发送到下一个 Activity - android

android - 如何从我自己的 Android 应用程序调用内置相机应用程序?

android - 位图文本与 native 缓存的比较