android mqtt paho客户端-npe教程代码

pcww981p  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(290)

目前,我尝试在android上使用mqtt。我正在使用api版本30,并尝试实现一个mqtt paho客户机来访问我家庭网络中raspberry pi上的mqtt代理。似乎最近android paho客户端出现了一些问题。至少,这是我在看了github回购协议之后的猜测,其中有很多未解决的问题和关于缺少维护的评论。
不管怎样,问题可能出在屏幕前几英寸处:-)。我要遵循的教程或说明可在此处找到:https://www.hivemq.com/blog/mqtt-client-library-enyclopedia-paho-android-service/
下面是触发空指针异常的代码片段。调试之后,我看不出我创建的对象中有一个真的是空的-奇怪吗?也许你们谁也有同样的问题?

// subscribe
    String topic = "test";
    int qos = 0;
    try {
        // Problematic line of code is following ...
        IMqttToken subToken = client.subscribe(topic, qos);
        subToken.setActionCallback(new IMqttActionListener() {
            @Override
            public void onSuccess(IMqttToken asyncActionToken) {
                // The message was published
                Log.i("MainActivity - Mqtt subscribe","success.");
            }
            @Override
            public void onFailure(IMqttToken asyncActionToken,
                                      Throwable exception) {
                    // The subscription could not be performed, maybe the user was not
                    // authorized to subscribe on the specified topic e.g. using wildcards
                    Log.i("MainActivity - Mqtt subscribe","failure.");
                }
            });
        } catch (MqttException e) {
            e.printStackTrace();
    }

java.lang.nullpointerexception:尝试调用虚拟方法'void org.eclipse.paho.android.service.mqttservice.subscribe(java.lang.string,java.lang.string,int,java.lang.string,位于org.eclipse.paho.android.service.mqttandroidclient.subscribe(mqttandroidclient)的空对象引用上。java:909)在org.eclipse.paho.android.service.mqttandroidclient.subscribe(mqttandroidclient。java:844)在net.jochenbauer.futurelivingenergydashboard.mainactivity.handledevent(mainactivity。java:85)在net.jochenbauer.futurelivingenergydashboard.mainactivity.access$000(mainactivity。java:26)在net.jochenbauer.futurelivingenergydashboard.mainactivity$1.onclick(mainactivity。java:40)

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题