2013-05-21 55 views
7

在使用示例和tutorial on the developer page第一次執行NSDManager期間,應用程序成功啓動了發現並找到了設備。在Android中使用網絡服務發現時發生內部錯誤

然而現在它似乎被打破...

當程序被啓動,一些初始化後,代碼進入下面的方法,併成功運行:

public void discoverServices() { 
    Log.d(TAG, "Initializing discovery on NSD"); 
    mNsdManager.discoverServices(
      SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener); 
} 

對數消息已收到。好一會兒後(說大約5分鐘),這是從程序輸出:

05-21 11:08:32.518: E/NsdCamera(12236): Discovery failed: Error code:0 
05-21 11:08:32.518: W/dalvikvm(12236): threadid=12: thread exiting with uncaught exception (group=0x40c9c930) 
05-21 11:08:32.518: E/AndroidRuntime(12236): FATAL EXCEPTION: NsdManager 
05-21 11:08:32.518: E/AndroidRuntime(12236): java.lang.NullPointerException 
05-21 11:08:32.518: E/AndroidRuntime(12236): at android.net.nsd.NsdManager$ServiceHandler.handleMessage(NsdManager.java:338) 
05-21 11:08:32.518: E/AndroidRuntime(12236): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-21 11:08:32.518: E/AndroidRuntime(12236): at android.os.Looper.loop(Looper.java:137) 
05-21 11:08:32.518: E/AndroidRuntime(12236): at android.os.HandlerThread.run(HandlerThread.java:60) 

從服務另外:

05-21 11:50:49.108: E/NativeDaemonConnector.ResponseQueue(8858): Timeout waiting for response 
05-21 11:50:49.108: E/mDnsConnector(8858): timed-out waiting for response to 10 mdnssd discover 6 _http._tcp. 
05-21 11:50:49.108: E/NsdService(8858): Failed to discoverServices com.android.server.NativeDaemonConnector$NativeDaemonFailureException: command '10 mdnssd discover 6 _http._tcp.' failed with 'null' 

錯誤代碼「0」被設置在NSDManager class描述爲一個內部錯誤。 我做的主要更新是訪問輔助類中稱爲NsdCamera的上下文。 下面是一些可能是邪惡的代碼片段:

助手類的構造函數:

public NsdCamera(CameraChooseActivity context) { 
    mContext = context; 
    updateUI = new UpdateUI(); 
    mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE); 
    mServiceName = new Vector<NsdServiceInfo>(); 

助手類NSD初始化:

public void initializeNsd() { 
    initializeDiscoveryListener(); 
} 

public void initializeDiscoveryListener() { 
    mDiscoveryListener = new NsdManager.DiscoveryListener() { 

     @Override 
     public void onDiscoveryStarted(String regType) { 
      Log.d(TAG, "Service discovery started"); 
     } 
     /** 
     * A name check to see if the DNS discovery was correct. Checks if it contains 
     * AXIS and has the desired MAC address-space 
     * @param hostname ,the inputted hostname from the discovery cycle 
     * @return true if it's an Axis camera. 
     */ 
     public boolean nameCheck(String hostname){ 
      return (hostname.contains("AXIS") && hostname.contains("00408C")); 

     } 
     @Override 
     public void onServiceFound(NsdServiceInfo service) { 
      Log.d(TAG, "Service discovery success: " + service.getServiceName()); 
      if (!service.getServiceType().equals(SERVICE_TYPE)) { 
       Log.d(TAG, "Unknown Service Type: " + service.getServiceType()); 
      } else if (nameCheck(service.getServiceName())){ 
       mServiceName.add(service); 
//     updateUI.execute(new BundleUI(mContext,service, null)); 
      } 
     } 

     @Override 
     public void onServiceLost(NsdServiceInfo service) { 
      Log.e(TAG, "service lost" + service); 
      if(mServiceName.remove(service)){ 
       //TODO 
       Log.e(TAG, "remove the view, service is lost"); 
      } 
     } 

     @Override 
     public void onDiscoveryStopped(String serviceType) { 
      Log.i(TAG, "Discovery stopped: " + serviceType); 
      //Necessary?? 
      mServiceName.clear(); 
     } 

     @Override 
     public void onStartDiscoveryFailed(String serviceType, int errorCode) { 
      Log.e(TAG, "Discovery failed: Error code:" + errorCode); 
      mNsdManager.stopServiceDiscovery(this); 
     } 

     @Override 
     public void onStopDiscoveryFailed(String serviceType, int errorCode) { 
      Log.e(TAG, "Discovery failed: Error code:" + errorCode); 
      mNsdManager.stopServiceDiscovery(this); 
     } 
    }; 
} 

CameraChooseActivity - >的onCreate是打電話給幫手級

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_camerachoose); 

    //Setup the animation for the text in the Relativelayout 
    mDescription = (TextSwitcher) findViewById(R.id.camera_add); 
    mDescription.setFactory(this); 
    mDescription.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); 
    mDescription.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); 
    mDescription.setText(getText(R.string.camera_add)); 

    //Building alert dialog 
    mBuilder = new AlertDialog.Builder(this,AlertDialog.THEME_HOLO_DARK); 
    mBuilder.setMessage(R.string.dialog_about).setTitle(R.string.action_about); 
    mBuilder.setIcon(android.R.drawable.ic_dialog_info); 

    mLayout = (RelativeLayout) findViewById(R.id.layout_camerachoose); 

    //Initialize the NSD 
    mNSDHelper = new NsdCamera(this); 
    mNSDHelper.initializeNsd(); 

回答

3

此基礎上我的經驗,我想這是一個監聽器壽命的問題。

因爲您爲系統NSD服務提供了兩個偵聽器,一個用於startServiceDiscovery(),另一個用於stopServiceDiscovery()。您需要確保這些偵聽器在系統訪問這些偵聽器時仍處於活動狀態。

一個事實是,在調用startServiceDiscovery()2分鐘後調用onStartDiscoveryFailed(),與偵聽器的生命週期相比,它應該是一個很長的時間。

因此,如果偵聽器是本地對象並在調用startServiceDiscovery()後被釋放,則可能導致NSD服務崩潰。

公共無效stopServiceDiscovery(NsdManager.DiscoveryListener 監聽器)與discoverServices發起

停止服務發現()。將激活的 服務發現通過 onDiscoveryStarted(字符串)通知給應用程序,並且它將保持活動狀態,直到應用程序 調用停止服務發現。通過調用onDiscoveryStopped(String)將成功停止通知給 。

一旦失敗停止服務發現,應用程序通過onStopDiscoveryFailed(String,int)通知 。

參數偵聽器這應該是通過 傳遞給discoverServices(String,int,NsdManager.DiscoveryListener)的偵聽器對象。它 標識應該停止的發現並通知 成功停止。

及以下代碼段確保不要調用任何NsdManager api。

@Override 
public void onStartDiscoveryFailed(String serviceType, int errorCode) { 
    Log.i(TAG, "onStartDiscoveryFailed : Error code:" + errorCode); 
} 

@Override 
public void onStopDiscoveryFailed(String serviceType, int errorCode) { 
    Log.i(TAG, "onStopDiscoveryFailed : Error code:" + errorCode); 
} 

祝你好運。

+0

好的答案!謝謝您的幫助。 那麼它實際上解決了空指針!但是,「內部錯誤」(錯誤代碼0)仍然存在:S – Mazze

3

簡單的DUT重啓被證明是解決方案。必須說這個錯誤很奇怪。 我認爲守護程序崩潰並沒有重新啓動。

(如果有人可以張貼的分析或有一個更好的解決方案,請張貼)

+0

什麼是DUT?我有同樣的錯誤,但我的設備重新啓動。 – fsschmitt

+0

DUT =被測設備。 許多人有這個問題,一些設備重啓,由於守護進程崩潰,但最小不... http://stackoverflow.com/questions/16907102/android-network-printers-discovery-nullpointerexception – Mazze

+0

不太確定如果這應該被標記爲正確的答案 –

相關問題