2011-12-02 56 views
2

我試圖做一個應用程序,它會檢測附近可發現的藍牙設備,並將所有的mac地址放入列表中。由於我不明白的原因,如果我嘗試使用列表來存儲來自device.getAddress()的字符串,應用程序會崩潰。如何將ArrayAdapter適配器放入ArrayList中?

在無法將這些地址放入List中後,我放棄並使用了ArrayAdapter,看到它在某些示例代碼中被使用。我的想法可能是我可以把結果放入一個ArrayAdapter,然後從中提取出地址,然後將其放入myList中。仍然無法工作。至於我可以告訴大家,我不能用鏈表沒有我的應用程序崩潰...

的應用程序不會崩潰,當我註釋掉任何事情做myList中。

/* these are declared as class members */ 
//private ArrayAdapter<String> myArrayAdapter; 
//private List<String> myList; 
/*-------------------------------*/ 

private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     int position = 0; 

     if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      myArrayAdapter.add(device.getAddress()); 
      //myList.add(device.getAddress()); 
      //position++; //SO USING THIS INTEGER... 

     } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {    
      btnLocate.setText("Refresh Location"); 
      btnLocate.setClickable(true); 
      //position--; 
      //while(position>=0){ 
       //myList.add(myArrayAdapater.getItem(position).toString()); 
       //position--; 
      //} 
     } 
    } 
}; 

的logcat:

12-02 12:01:54.472: E/AndroidRuntime(16043): FATAL EXCEPTION: main 
12-02 12:01:54.472: E/AndroidRuntime(16043): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND (has extras) } in [email protected] 
12-02 12:01:54.472: E/AndroidRuntime(16043): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722) 
12-02 12:01:54.472: E/AndroidRuntime(16043): at android.os.Handler.handleCallback(Handler.java:587) 
+0

我是...在這個類型的noob ...什麼是logcat,我怎麼能得到它? – Szuturon

+1

不是聽起來像一個混蛋,但[它是第一次在谷歌](https://www.google.com/search?q=android+logcat)... http://developer.android.com/guide /developing/tools/logcat.html。 –

+0

在這裏發佈您的例外。轉到DDMS(**,即窗口>顯示視圖>其他> DDMS **)並選擇紅色的日誌貓行,在這裏複製並粘貼。 –

回答

0

你沒有嘗試在設備地址添加到它正確初始化之前您的名單?此外,您可能需要閱讀Receiver生命週期,因爲開發人員文檔討論對象有效期限有多長。 BroadcastReceiver#ReceiverLifecycle