【问题标题】:App crashes when BackButton is pressed按下返回按钮时应用程序崩溃
【发布时间】:2014-11-21 12:39:15
【问题描述】:

我正在使用Paho Android Service API,我在onResume 中连接并在onPause 中断开连接。在运行时,我可以通过设备subscribe 并通过mosquitto Broker 看到来自PC 的published 消息。当我按下他BackButton 时,应用程序崩溃并显示以下logcat 错误。

请告诉我如何解决这个问题。

注意

1_因为我使用的是Paho Android Service API,所以我将所需的库导入到项目中。和
在我的项目清单文件的application 元素中,我添加了这一行

2_我没有在我的框架中注册任何接收器,接收器包含在我的库中 导入以使用 Paho API

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

LogCat

     Activity com.example.mqtt_test_00.MessaginActivity   
    has leaked IntentReceiver org.eclipse.paho.android.service.MqttAndroidClient@43075698 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  
    org.eclipse.paho.android.service.MqttAndroidClient@43075698 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.eclipse.paho.android.service.MqttAndroidClient$MyServiceConnection@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 )

【问题讨论】:

  • 注销服务,在你回压类之前,在 onpause() 方法中使用注销

标签: android service mqtt paho


【解决方案1】:

logcat 非常清楚情况。您似乎没有取消注册接收器。

您应该通过在您的 Activity 拥有的生命周期方法之一(onPause()、onStop())中调用 unregisterReceiver() 来做到这一点。

unregisterReceiver(yourReceiver); 

yourReceiver 很可能是 MqttAndroidClient 的一个实例(每个 logcat 输出)

【讨论】:

  • 我没有在我的框架中注册任何接收器,该接收器包含在我导入以使用 Paho API 的库中
【解决方案2】:

你的问题是:

11-21 13:26:51.703: E/ActivityThread(28310): android.app.IntentReceiverLeaked: Activity   
com.example.mqtt_test_00.MessaginActivity has leaked IntentReceiver  
org.eclipse.paho.android.service.MqttAndroidClient@43075698 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();
    }
}

【讨论】:

  • 我没有在我的框架中注册任何接收器,该接收器包含在我导入以使用 Paho API 的库中
  • 您的 logcat 显示已实现的消息接收器服务,然后在您退出课程后,您不会取消注册该服务,
  • 好的,您在清单文件中显示服务以及使用注册意图服务的位置
  • 关注这个链接:git.eclipse.org/c/paho/org.eclipse.paho.mqtt.java.git/…,这个链接可以帮助你了解寄存器
【解决方案3】:

我不会重复其他成员的建议和建议,但我唯一想说的是,调用client.disconnectunregister service。你给disconnect打过电话吗?

【讨论】:

    【解决方案4】:

    MqttAndroidClient 有一个名为 unregisterResources 的方法,如果有的话,它将取消注册接收者。

    用法是这样的

    MqttConnection 连接 = MqttConnections.getInstance(this).getConnection(clientHandle); connection.getClient().unregisterResources();

    【讨论】:

      猜你喜欢
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多