Android蓝牙项目中的“mArrayAdapter无法解析”。

时间:2012-03-24 19:56:43

标签: java android arrays string bluetooth

我正在为2.2平台上的android开发蓝牙应用程序。我的代码中有两个mArrayAdapter实例,但这两个实例都无法解析。我的代码几乎与Android开发站点上的代码完全相同,因为我以它为例。我尝试在本地定义变量,但是在mArrayAdapter之后我在.add上出现错误。我发现有类似问题的文章,但他们的答案都没有对我有用。我认为我必须在某个地方定义它,但没有关于我在网上找到的任何记号。我将在下面粘贴一些代码。感谢。

     //Find the paired Devices
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
//If there are paired devices
if (pairedDevices.size() > 0) {
 // Loop through paired devices
 for (BluetoothDevice device : pairedDevices) {
     // Add the name and address to an array adapter to show in a ListView
     //---------------------> ERROR BELOW <------------------------
     mArrayAdapter.add(device.getName() + "\n" + device.getAddress());

 }
}
// Discovering Bluetooth Devices.
final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        public void onReceive (Context context, Intent intent) {
        String action = intent.getAction();
        //When discovery finds a device
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            //Get the BluetoothDevice object from the Intent
            BluetoothDevice device =   intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            //Log.v("bluetooth Tesing",device.getName() + "\n" + device.getAddress());
            // Add the name and address to an array adapter to show in ListView. 
            //---------------------> ERROR BELOW <------------------------
            mArrayAdapter.add(device.getName() +"\n" + device.getAddress());
        }
    }
}; 

1 个答案:

答案 0 :(得分:0)

在您用作模板的示例蓝牙聊天,DeviceListActivity中,您将看到两个 ArrayAdapter适配器都被声明为类中的成员变量(因此'm'前缀)靠近班级的顶部。它们都用

实例化
 m?????ArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name);
在onCreate中

。在尝试向其添加项目之前,您需要为一个适配器做一些类似的事情。