2012-03-24 49 views
0

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

 //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()); 
     } 
    } 
}; 

回答

0

在样品蓝牙聊天,DeviceListActivity您正在使用的templete,你会看到 ArrayAdapter适配器都被声明为类(因此“M”字头)成员变量附近这是班级的顶峰。他们都与

m?????ArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name); 
在OnCreate

实例化。在尝试向其中添加项目之前,您需要为您的一个适配器执行一些操作。

+0

我添加了您指定的代码,但必须将device_name更改为main。 Eclipse只会让我在那里使用main。这解决了我的问题与mArrayAdapter。现在我遇到了一些其他行有错误“未处理的异常类型IOException”的问题。我自己挖了一遍,但找不到解决方案。我不明白为什么更改不相关变量的声明会影响其他多行代码。 – user1290423 2012-03-25 13:20:15