int i = 0; String[] Dname = new String[5]; String[] Daddress = new String[5]; // 获取已配对蓝牙设备 Set<BluetoothDevice> paired = mbluetooth.getBondedDevices(); if(paired.size() > 0){ for(BluetoothDevice device:paired) { String deviceName = device.getName(); String deviceAddress = device.getAddress(); Dname[i] = deviceName; Daddress[i] = deviceAddress; i++; Toast.makeText(MainActivity.this,Dname[0],Toast.LENGTH_SHORT).show(); } System.out.println(Dname[0]); String[] nameArr = new String[]{"Arun"}; //,"Anil","Ankit","Manoj" ArrayAdapter<String> adp = new ArrayAdapter<String >(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, Dname); //这里换成nameArr就正常,用Dname就出错 ListView lvofpaired = (ListView) findViewById(R.id.LV_PairedDevices); lvofpaired.setAdapter(adp); System.out.println(Daddress[0]);
两个输出:
I/System.out: q_bluetooth
I/System.out: 00:01:17:00:16:00
这段是获取已 经 蓝牙配对的设备 并添加到ListView里。
取配对设备名和地址正常,只是在
ArrayAdapter<String> adp = new ArrayAdapter<String >(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, Dname); //这里换成nameArr就正常,用Dname就出错
错误是:java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
不明白怎么回事? 为什么用数组不出错,用我动态取到的就空对像了
谢谢
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How can I create this custom Bottom Navigation on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
sdk自带的android.R.layout.simple_list_item_1好像是只支持String类型的数据,不支持自定义的数据(你这里自定义了DName),如果你想使用自定义数据,可以看一下android.widget.BaseAdapter的用法,不过现在列表更多都是在用RecyclerView了