theButton = (Button) findViewById(R.id.button5);
client = new SocketIOClient(URI.create("http://192.168.0.157:8080"), new SocketIOClient.Handler() {
@Override
public void onConnect() {
Log.e("WS", "Connected!");
}
@Override
public void on(String event, JSONArray arguments) {
theButton.setBackgroundColor(Color.BLACK);
Log.e("WS", "Got event "+event+":"+arguments.toString());
}
@Override
public void onDisconnect(int code, String reason) {
Log.e("WS", "Disconnected! Code: "+code+" Reason:"+reason);
}
@Override
public void onError(Exception error) {
Log.e("WS", "+++ Error +++"+error);
}
});
client.connect();
的問題是,如果我在事件的Funktion更改BACKGROUNDCOLOR我得到一個空指針exeption:機器人的WebSocket socket.io空指針
E/AndroidRuntime(16409):致命異常:螺紋11864 E/AndroidRuntime(16409):java.lang.NullPointerException E/AndroidRuntime(16409):at com.fritsch.cpu.SocketIOClient.cleanup(SocketIOClient.java:183) E/AndroidRuntime(16409):at com.fritsch .cpu.SocketIOClient.access $ 0(SocketIOClient.java:181) E/AndroidRuntime(16409):at com.fritsch.cpu.SocketIOClient $ 2.onError(SocketIOCli ent.java:152) E/AndroidRuntime(16409):at com.fritsch.cpu.WebSocketClient $ 1.run(WebSocketClient.java:125) E/AndroidRuntime(16409):at java.lang.Thread.run(Thread .java:856)
如果我在初始化後更改BG顏色,它工作正常。我認爲問題是WebSocket的線程。
你在'findViewById'之前完成了'setContentView'嗎? – vinayr