我正在尝试向我的MQTT代理(我的Raspberry Pi)发布一条消息,主题为rpi / gpio。发送该消息的目的是打开/关闭连接到我的Raspberry Pi的LED(如this教程,但该教程适用于Swift而不是Android)。因此,当我打开Android应用程序上的切换小部件时,消息“on”将发布到Raspberry Pi代理以打开它(通过也连接到Raspberry Pi上的MQTT的python脚本)。 没有所有MQTT代码,下面的片段通过显示开关很好地工作。 但是,使用以下代码时,它会崩溃。
public class ControlsFragment extends Fragment {
private ControlsFragment.OnFragmentInteractionListener listener;
public static ControlsFragment newInstance() {
return new ControlsFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_controls, container, false);
Switch switch1= (Switch)view.findViewById(R.id.tempSwitch);
switch1.setChecked(true);
switch1.setTextOn("On");
switch1.setTextOff("Off");
String clientId = MqttClient.generateClientId();
MqttAndroidClient client =
new MqttAndroidClient(this.getActivity(), "tcp://192.168.100.8:1883",
clientId);
try {
IMqttToken token = client.connect();
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
// We are connected
Log.d(TAG, "onSuccess");
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
// Something went wrong e.g. connection timeout or firewall problems
Log.d(TAG, "onFailure");
}
});
} catch (MqttException e) {
e.printStackTrace();
}
String topic = "rpi/gpio";
String payload;
if(switch1.isChecked()){
payload="on";
}
else{
payload="off";
}
byte[] encodedPayload = new byte[0];
try {
encodedPayload = payload.getBytes("UTF-8");
MqttMessage message = new MqttMessage(encodedPayload);
client.publish(topic, message);
} catch (UnsupportedEncodingException | MqttException e) {
e.printStackTrace();
}
return view;
}
@Override
public void onAttach(android.content.Context context) {
super.onAttach(context);
if (context instanceof ControlsFragment.OnFragmentInteractionListener) {
listener = (ControlsFragment.OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
listener = null;
}
public interface OnFragmentInteractionListener {
}
}
Logcat错误:
03-07 00:40:37.537 32415-32415/com.example.teerna.smartagriculturev5 E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at org.eclipse.paho.android.service.MqttAndroidClient.publish(MqttAndroidClient.java:815)
at org.eclipse.paho.android.service.MqttAndroidClient.publish(MqttAndroidClient.java:673)
at com.example.teerna.smartagriculturev5.ControlsFragment.onCreateView(ControlsFragment.java:90)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5283)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
com.example.teerna.smartagriculturev5.ControlsFragment.onCreateView(ControlsFragment.java:90)
指向以下行:
client.publish(topic, message);
,可在try
块中找到。
使用以下参考:
https://www.hivemq.com/blog/mqtt-client-library-enyclopedia-paho-android-service
答案 0 :(得分:5)
如果您遵循paho源代码中的错误,您将发现NPE是由客户端的mqttService null
引起的。您需要等待在发布消息之前建立与服务的连接,正确的位置将是回调的onSuccess
,您基本上忽略了
token.setActionCallback(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
// after this is called you can publish messages
}
@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
// Something went wrong e.g. connection timeout or firewall problems
Log.d(TAG, "onFailure");
}
});