2012-04-29 37 views
1

我想找到附近的藍牙設備,所以我使用BroadcastReceiver。大多數時候它工作正常,但有時我得到這個錯誤。接收藍牙發現的廣播錯誤

04-30 09:50:15.277: E/AndroidRuntime(847): FATAL EXCEPTION: main 
04-30 09:50:15.277: E/AndroidRuntime(847): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.bluetooth.device.action.FOUND (has extras) } in [email protected] 
04-30 09:50:15.277: E/AndroidRuntime(847): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722) 
04-30 09:50:15.277: E/AndroidRuntime(847): at android.os.Handler.handleCallback(Handler.java:587) 
04-30 09:50:15.277: E/AndroidRuntime(847): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-30 09:50:15.277: E/AndroidRuntime(847): at android.os.Looper.loop(Looper.java:130) 
04-30 09:50:15.277: E/AndroidRuntime(847): at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-30 09:50:15.277: E/AndroidRuntime(847): at java.lang.reflect.Method.invokeNative(Native Method) 
04-30 09:50:15.277: E/AndroidRuntime(847): at java.lang.reflect.Method.invoke(Method.java:507) 
04-30 09:50:15.277: E/AndroidRuntime(847): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-30 09:50:15.277: E/AndroidRuntime(847): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-30 09:50:15.277: E/AndroidRuntime(847): at dalvik.system.NativeStart.main(Native Method) 
04-30 09:50:15.277: E/AndroidRuntime(847): Caused by: java.lang.NullPointerException 
04-30 09:50:15.277: E/AndroidRuntime(847): at com.waratah.app.SetMachineActivity$2.onReceive(SetMachineActivity.java:532) 
04-30 09:50:15.277: E/AndroidRuntime(847): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709) 
04-30 09:50:15.277: E/AndroidRuntime(847): ... 9 more 

這表明NullPointerException異常,但我不明白怎麼能發生這種錯誤,因爲我檢查變量,以避免例外。

// The BroadcastReceiver that listens for discovered devices and 
// changes the title when discovery is finished 
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent == null) { 
      return; 
     } 
     String action = intent.getAction(); 
     int i; 
     Machine d; 

     if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { 
      if (mBtAdapter.isEnabled()) { 
       if(D) Log.d(BroadcastReceiver.class.getName(), "Bluetooth is enabled"); 
       pd.dismiss();     
      } 
      doDiscovery(); 

     // when bluetooth device is found 
     } else if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      if(D) Log.d(BroadcastReceiver.class.getName(), "found"); 
      // Get the BluetoothDevice object from the Intent 
      BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      if (device != null) { 

       // Check if the device has name 
       if ((device.getName() != null)||(device.getName().length() > 0)) { //LINE 532 HERE 
        d = new Machine(device); 
        if (d.getPairingState() != BluetoothDevice.BOND_BONDED) { 
         d.setBluetoothState(true); 
         addDevice(d); 
        } else { 
         for (i = 0; i < adapter.getCount(); i++) { 
          if (adapter.getItem(i).getAddress().equals(device.getAddress())) { 
            adapter.getItem(i).setBluetoothState(true); 
          } 
         } 
        } 
       } 
      } 
     // When discovery is finished, change the Activity title 
     } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { 
      scanButton.setVisibility(View.VISIBLE); 

      pb.setVisibility(View.GONE); 
      scanning.setVisibility(View.GONE); 
     } 
    } 
}; 

我已經註冊了我的所有操作並正確註冊了我的接收器。此錯誤偶爾會發生。 onReceive()函數在沒有任何東西可以被接收時觸發嗎?

感謝您的幫助。

編輯:532 線是

if ((device.getName() != null)||(device.getName().length() > 0)) { 

所以這意味着設備必須是空的,但這樣做的前一行的檢查。

if (device != null) { 

順便說一下,兩個廣播接收機同時運行會導致這種錯誤嗎?廣播接收器收到後是否爲空?

+1

啊,什麼SPECIFIC行有NullPointerException?你真的可以突出顯示第532行,這是發生異常的地方。 – Femi

+0

@Femi我無法突出顯示代碼行,因此我添加了一條評論,並單獨引用了532行。 – Mira

回答

2

我懷疑你誤用了||而不是& &。

if ((device.getName() != null)||(device.getName().length() > 0)) {

if ((device.getName() != null) && (device.getName().length() > 0)) {

作爲原始寫入,如果device.getName()爲空,則它試圖評估的第二部分,這是在呼叫長度()空值,從而導致你看到的NPE。

+0

哦,你是對的!像這樣簡單的錯誤很難找到......謝謝,我會糾正它,並再次嘗試! – Mira