Android 我无法在新线程(new Runnable()中看到我的Toast消息
我在连接和断开连接后显示消息方面有点问题 我正在尝试按下我的连接按钮并在新线程中建立通信以获取toast消息:您已连接,如果我未连接,则在异常中获取toast消息:您未连接 我试着把新的线换成新的Android 我无法在新线程(new Runnable()中看到我的Toast消息,android,multithreading,exception,tcp,toast,Android,Multithreading,Exception,Tcp,Toast,我在连接和断开连接后显示消息方面有点问题 我正在尝试按下我的连接按钮并在新线程中建立通信以获取toast消息:您已连接,如果我未连接,则在异常中获取toast消息:您未连接 我试着把新的线换成新的 runOnUiThread(新的Runnable()/*但是它在下划线下*/})。start();在我的代码末尾是一个错误。当我删除start并运行应用程序时,它无法连接到我的服务器 我尝试了很多解决方案,但都不管用。如果您能帮助我,我将非常感激 @Override public void on
runOnUiThread(新的Runnable()/*但是它在下划线下*/})。start();
在我的代码末尾是一个错误。当我删除start并运行应用程序时,它无法连接到我的服务器
我尝试了很多解决方案,但都不管用。如果您能帮助我,我将非常感激
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try {
server = serverTxt.getText().toString();
Socket socket = new Socket(server, port);
socket.setSoTimeout(30000);
connected = true;
Toast.makeText(getApplicationContext(), "You are connected",Toast.LENGTH_LONG).show();
//send the message to the server
here = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "You are not connected",Toast.LENGTH_LONG).show();
}
}
}).start();
}
});
您必须在UI线程上显示祝酒词。您可以使用处理程序来执行此操作
Handler handler = new Handler(Looper.getMainLooper());
handler.postRunnable(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "You are connected",Toast.LENGTH_LONG).show();
}
});
您必须在UI线程上显示祝酒词。您可以使用处理程序来执行此操作
Handler handler = new Handler(Looper.getMainLooper());
handler.postRunnable(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "You are connected",Toast.LENGTH_LONG).show();
}
});
您必须按如下方式使用runOnUiThread: 不要尝试在UI线程中使用套接字部分,它将引发异常
new Thread(new Runnable() {
@Override
public void run() {
try {
server = serverTxt.getText().toString();
Socket socket = new Socket(server, port);
socket.setSoTimeout(30000);
connected = true;
//if inside a fragment, call with getActivity().runOnUiThread()
Activity.this.runOnUiThread(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "You are connected",Toast.LENGTH_LONG).show();
}
});
//send the message to the server
here = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
} catch (Exception e) {
//if inside a fragment, call with getActivity().runOnUiThread()
Activity.this.runOnUiThread(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "You are not connected",Toast.LENGTH_LONG).show();
}
});
}
}
}).start();
runOnUiThread是一种方法。.start()仅适用于线程对象,用于启动新线程。您希望您的ui(已运行)线程能够集中注意力:)您必须按如下方式使用runOnUiThread: 不要尝试在UI线程中使用套接字部分,它将引发异常
new Thread(new Runnable() {
@Override
public void run() {
try {
server = serverTxt.getText().toString();
Socket socket = new Socket(server, port);
socket.setSoTimeout(30000);
connected = true;
//if inside a fragment, call with getActivity().runOnUiThread()
Activity.this.runOnUiThread(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "You are connected",Toast.LENGTH_LONG).show();
}
});
//send the message to the server
here = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
} catch (Exception e) {
//if inside a fragment, call with getActivity().runOnUiThread()
Activity.this.runOnUiThread(new Runnable(){
public void run(){
Toast.makeText(getApplicationContext(), "You are not connected",Toast.LENGTH_LONG).show();
}
});
}
}
}).start();
runOnUiThread是一种方法。start()仅适用于线程对象,用于启动新线程。在这里,您希望您的ui(已经运行)线程获得关注:)您应该在方法
runOnUiThread
中使用它
例如:
activity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Message", Toast.LENGTH_SHORT).show();
}
});
您应该在方法
runOnUiThread
中使用它
例如:
activity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Message", Toast.LENGTH_SHORT).show();
}
});
考虑使用异步任务。在onPostExecute中,您可以访问UI并发出Toast通知
private class operation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
server = serverTxt.getText().toString();
Socket socket = new Socket(server, port);
socket.setSoTimeout(30000);
connected = true;
return "You are connected";
//send the message to the server
here = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
} catch (Exception e) {
return "You are not connected"
}
return null;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
私有类操作扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
server=serverTxt.getText().toString();
套接字=新的套接字(服务器、端口);
插座。设置插座(30000);
连接=真;
返回“您已连接”;
//将消息发送到服务器
此处=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()),true);
}捕获(例外e){
返回“您未连接”
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG.show();
}
}
考虑使用AsyncTask。在onPostExecute中,您可以访问UI并发出Toast通知
private class operation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
server = serverTxt.getText().toString();
Socket socket = new Socket(server, port);
socket.setSoTimeout(30000);
connected = true;
return "You are connected";
//send the message to the server
here = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
} catch (Exception e) {
return "You are not connected"
}
return null;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
私有类操作扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
server=serverTxt.getText().toString();
套接字=新的套接字(服务器、端口);
插座。设置插座(30000);
连接=真;
返回“您已连接”;
//将消息发送到服务器
此处=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()),true);
}捕获(例外e){
返回“您未连接”
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG.show();
}
}
是否要在片段中执行此操作?1)runOnUiThread调用后不需要.start()。2) 尝试在runOnUiThread中使用套接字代码可能会引发NetworkOnMainThread异常->仅使用toast部分是否尝试在片段中执行此操作?1)runOnUiThread调用后不需要.start()。2) 尝试在runOnUiThread中使用套接字代码可能会引发NetworkOnMainThread异常->仅使用toast部分不使用uipostRunnable的应用程序上下文是UnderlineEdit应该只是post(),抱歉。我尝试过,但它不起作用。我的代码是片段,我也有NFC通信。我不知道是不是有什么不同不要使用uipostRunnable的应用程序上下文是下划线编辑应该是post(),对不起。我试过了,但它不起作用。我的代码是片段,我也有NFC通信。我不知道有什么区别。我试过代码,但当我没有连接时,不会出现问题。我在哪里可以使用插座部分?查看完整答案。这适用于您:)您未连接将仅在套接字引发异常时显示。否则它不会显示我尝试了代码,但当我未连接时,不会显示Toast我在哪里可以使用插座部分?查看完整答案。这适用于您:)您未连接将仅在套接字引发异常时显示。否则它就不会出现