2012-03-24 36 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