0

我是MQTT新手paho Libs for Android。今天我尝试创建一个简单的连接示例,如下所示,但是发生的情况是,logcat尽管代码中没有错误,但在运行时,我从下面发布的错误消息中得到了 eclipse 说can not run the projects due to errors "which are not exist"或应用程序崩溃,我得到了下面发布的错误日志。

此外,我在我的项目中的库图片下方发布了一张图片,这些库是从paho website.

请让我知道我错过了什么以及我做错了什么。

代码

public class MQTT_00 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mqtt_00_layout);

        MqttClientPersistence persistence = null;
        try {
            persistence = new MqttDefaultFilePersistence("/temp/");
        } catch (MqttPersistenceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            MqttClient client = new MqttClient("tcp://localhost:1883", "MQTTSub", persistence);
            MqttConnectOptions opts = new MqttConnectOptions();
            opts.setKeepAliveInterval(480);
            opts.setWill(client.getTopic("willTopic"), "something bad happened".getBytes(), 1, true);
            client.connect(opts);
        } catch (MqttException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

}

日志猫

11-10 12:52:49.617: E/AndroidRuntime(30700): FATAL EXCEPTION: main
11-10 12:52:49.617: E/AndroidRuntime(30700): Process: com.example.mqtt_00, PID: 30700
11-10 12:52:49.617: E/AndroidRuntime(30700): java.lang.NoClassDefFoundError:  
org.eclipse.paho.client.mqttv3.MqttDefaultFilePersistence
11-1 12:52:49.617: E/AndroidRuntime(30700):     at 
com.example.mqtt_00.MQTT_00.onCreate(MQTT_00.java:21)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at   
android.app.Activity.performCreate(Activity.java:5389)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at  
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
 11-10 12:52:49.617: E/AndroidRuntime(30700):   at 
android.app.ActivityThread.access$800(ActivityThread.java:157)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at 
android.os.Handler.dispatchMessage(Handler.java:102)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at android.os.Looper.loop(Looper.java:157)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at 
android.app.ActivityThread.main(ActivityThread.java:5293)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at java.lang.reflect.Method.invokeNative(Native 
Method)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at 
java.lang.reflect.Method.invoke(Method.java:515)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at c   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
11-10 12:52:49.617: E/AndroidRuntime(30700):    at dalvik.system.NativeStart.main(Native Method)

图片 在此处输入图像描述

4

1 回答 1

0

通过在 Java 构建路径-> 库中添加外部 Jar 并随后按顺序检查库和导出选项卡,确保您正确添加了库。完成后,清理项目并尝试再次运行。这应该可以解决问题!

它在我的最后工作。

于 2014-12-09T15:01:02.353 回答