2012-02-05 54 views
1

我正在學習Android上使用Wrox 專業Android 2應用程序開發書的藍牙編程。發現監控器示例(第432)有這樣的代碼片段:Android:監測藍牙發現 - 這個編譯錯誤是什麼意思?

BroadcastReceiver discoveryMonitor = new BroadcastReceiver() { 

      String dStarted = BluetoothAdapter.ACTION_DISCOVERY_STARTED; 
      String dFinished = BluetoothAdapter.ACTION_DISCOVERY_FINISHED; 

      @Override 
      public void onReceive(Context context, Intent intent) { 
      if (dStarted.equals(intent.getAction())) { 
       // Discovery has started. 
       Toast.makeText(getApplicationContext(), 
          "Discovery Started...", Toast.LENGTH_SHORT).show(); 
      } 
      else if (dFinished.equals(intent.getAction())) { 
       // Discovery has completed. 
       Toast.makeText(getApplicationContext(), 
          "Discovery Completed...", Toast.LENGTH_SHORT).show(); 
      } 
      }  
     }; 
     registerReceiver(discoveryMonitor, 
         new IntentFilter(dStarted)); 
     registerReceiver(discoveryMonitor, 
         new IntentFilter(dFinished)); 

...並在每個兩個最後我得到registerReceiver調用的。 。 。

上標記語法錯誤,AnnotationName有望代替
語法錯誤,插入「類型VariableDeclaratorId」完成FormalParameterList

什麼是annotationName和什麼錯嗎?

在此先感謝!

回答

0

你的問題是你定義的變量dStarteddFinished作爲當地人discoveryMonitor廣播接收器, 這樣,你不能在registerReceiver使用它們(...)

你必須將其定義爲全球性的,或使用

registerReceiver(discoveryMonitor, new 
       IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED)); 

代替。