我对 Android studio 还很陌生,但到目前为止,一切都很好!
我有一个 Android 应用程序,它与我的计算机上运行的 websocket 一起启动并运行。 因此,当 websocket 和应用程序都启动并运行时。
我可以编写一条消息,然后将其打印在 websocket 终端中,并且 websocket 还会返回一条消息,并将其打印在我手机上的列表中。此外,如果 websocket 收到来自应用程序的特殊消息,websocket 还会返回一条特殊消息,该消息将显示并添加到列表中!完美的!不是吗?
现在我想做的是,应用程序监听来自 websocket 的特殊消息/事件,在本例中为“SuperSpecialMessage”。如果 websocket 发送此特定消息,则应用程序 会使用react并做某事。在这种情况下,使不可见按钮可见。
问题是什么?这里的问题是我收到错误:
“android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建 View 层次结构的原始线程才能触摸其 View 。”
有人可以帮我吗?
这是我的代码:
public class WebSocketActivity extends AppCompatActivity {
private Socket mSocket;
private TextView mSocketStatus;
EditText messageTextview;
ListView the_list_view_on_layout;
ArrayAdapter arrayAdapter;
String TheMessages;
List<String> list_with_messages;
Button theExtraButton;
Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_websocket);
this.mHandler = new Handler();
m_Runnable.run();
// Elements that are created on the layout
mSocketStatus = (TextView)findViewById(R.id.socketStatus);
theExtraButton = (Button)findViewById(R.id.extraBtn);
theExtraButton.setVisibility(View.INVISIBLE);
the_list_view_on_layout = findViewById(R.id.listViewID);
messageTextview = findViewById(R.id.messageTextView);
String[] TheMessages = new String[]{};
list_with_messages = new ArrayList<String>(Arrays.asList(TheMessages));
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_with_messages);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list_with_messages);
the_list_view_on_layout.setAdapter(arrayAdapter);
TheSocketClass app = (TheSocketClass) getApplication();
mSocket = app.getSocket();
mSocket.connect();
checkIfConnectedToSocket();
Button sendMessageToSocketServer = (Button) findViewById(R.id.sendMessageToSocketBtn);
sendMessageToSocketServer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String theTextWeWrite = messageTextview.getText().toString();
mSocket.emit("sendTextToServer", theTextWeWrite);
list_with_messages.add(theTextWeWrite);
//arrayAdapter.notifyDataSetChanged();
}
});
runOnUiThread(new Runnable() {
@Override
public void run() {
mSocket.on("ServerMessage", new Emitter.Listener() {
@Override
public void call(Object... args) {
JSONObject data = (JSONObject) args[0];
list_with_messages.add(data.toString());
//arrayAdapter.notifyDataSetChanged(); // I GET ERROR HERE
}
});
}
});
runOnUiThread(new Runnable() {
@Override
public void run() {
mSocket.on("SuperSpecialMessage", new Emitter.Listener() {
@Override
public void call(Object... args) {
theExtraButton.setVisibility(View.VISIBLE);
}
});
}
});
}
private void checkIfConnectedToSocket(){
mSocket.connect();
if (mSocket.connected() == true) {
mSocketStatus.setText("Connected to socket!");
mSocketStatus.setTextColor(Color.GREEN);
}
else if (!mSocket.connected()){
mSocketStatus.setText("Error connecting to socket!");
mSocketStatus.setTextColor(Color.RED);
}
}
}
谢谢。
最佳答案
编辑:解决了!
首先我做了一个这样的方法:
private void makeTheButtonVisible(){
runOnUiThread(new Runnable() {
@Override
public void run() {
theExtraButton.setVisibility(View.VISIBLE);
}
});
}
当应用程序收到消息时,会像这样调用该方法:
runOnUiThread(new Runnable() {
@Override
public void run() {
mSocket.on("SuperSpecialMessage", new Emitter.Listener() {
@Override
public void call(Object... args) {
makeTheButtonVisible();
}
});
}
});
关于java - 当我的应用程序从 websocket 触发某些内容时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59029929/