2014-11-21 104 views
1

我使用Paho Android Service API,我在onResume中連接,並在中斷開連接。在運行時,我可以從設備上獲取subscribe,並通過mosquitto Broker查看來自PC的published消息。當我按他BackButton該應用程序crahses並顯示下面logcat錯誤。當按下BackButton時,應用程序崩潰

請讓我知道如何解決這個問題。

到注意

1_since我使用Paho Android Service API我進口所需的庫到項目中。而
我的項目的清單文件的application元素內我已經加入這一行

2_i不是在我的frament註冊任何接收器,在包括在庫接收我 進口使用泛美衛生組織API

<!-- Mqtt Service --> 
    <service android:name="org.eclipse.paho.android.service.MqttService" > 
    </service> 

logcat的

 Activity com.example.mqtt_test_00.MessaginActivity 
    has leaked IntentReceiver [email protected] that was 
    originally registered here. Are you missing a call to unregisterReceiver()? 
android.app.IntentReceiverLeaked: Activity 
    com.example.mqtt_test_00.MessaginActivity has leaked IntentReceiver 
    [email protected] that was originally registered here. 
    Are you missing a call to unregisterReceiver()? 
    at android.app.LoadedApk$ReceiverDispatcher. 
    <init>(LoadedApk.java:814) 
    at 
    android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:610) 
    at 
    android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1762) 
    at 
    android.app.ContextImpl.registerReceiver(ContextImpl.java:1742) 
    at 
    android.app.ContextImpl.registerReceiver(ContextImpl.java:1736) 
    at 
    android.content.ContextWrapper.registerReceiver(ContextWrapper.java:478) 
    at org 
    .eclipse.paho.android.service.MqttAndroidClient.registerReceiver(MqttAndroidClient.java:410) 

    at 
    org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:388) 
    at 
    com.example.mqtt_test_00.Sub_Frag$5.onClick(Sub_Frag.java:98) 
android.app.ServiceConnectionLeaked: Activity 
    com.example.mqtt_test_00.MessaginActivity has leaked ServiceConnection 
    org.ecl[email protected]430756f0 that was 
    originally bound here 
    at android.app.LoadedApk$ServiceDispatcher.<init> 
    (LoadedApk.java:988) 
    at 
    org.eclipse.paho.android.service.MqttAndroidClient.connect(MqttAndroidClient.java:385) 
    at 
    com.example.mqtt_test_00.Sub_Frag$5.onClick(Sub_Frag.java:98) 
+0

取消註冊該服務,在壓倒之前,在onpause()方法中使用unregister – prakash 2014-11-21 12:43:05

回答

1

的logcat的是瞭解情況非常清楚。好像你沒有註銷一個接收器。

您應該通過在您的Activity的生命週期方法(onPause(),onStop())之一中調用unregisterReceiver()來做到這一點。

unregisterReceiver(yourReceiver); 

哪裏yourReceiver是最有可能MqttAndroidClient的一個實例(每logcat的輸出)

+0

我沒有註冊任何接收器在我的frament中,接收器包含在我導入的庫中使用Paho API – user2121 2014-11-21 12:45:40

+0

您能否提供一些代碼,所以我可以看看? – Mike 2014-11-21 13:00:00

0

您的問題是:

11-21 13:26:51.703: E/ActivityThread(28310): android.app.IntentReceiverLeaked: Activity 
com.example.mqtt_test_00.MessaginActivity has leaked IntentReceiver 
[email protected] that was originally  registered here. 
Are you missing a call to unregisterReceiver()? 

你不是在同一個類中取消註冊服務,讓您有這個問題,嘗試這樣,

@Override 
public void onPause() { 
    super.onPause(); 
    try 
    { 
     unregisterReceiver(this.intentReceiver); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
+0

我沒有在我的檔案中註冊任何接收器,接收器包含在我導入的庫中,使用Paho API – user2121 2014-11-21 12:46:17

+0

您的logcat顯示消息接收器服務已實施,然後在您退出課程之後,您不註銷該服務, – prakash 2014-11-21 12:54:20

+0

好吧,您在清單文件中顯示的服務以及使用註冊intentservice – prakash 2014-11-21 12:56:48

0

我不會重複其他成員建議和重新但我唯一想說的是,撥打client.disconnectunregisterservice。你有沒有撥打disconnect

0

MqttAndroidClient有一個名爲unregisterResources的方法,它將取消註冊接收方(如果有的話)。

用法是喜歡的東西這

MqttConnection連接= MqttConnections.getInstance(本).getConnection(clientHandle); connection.getClient()。unregisterResources();