我在 new Thread(new Runnable()) 中看不到我的 Toast 消息

I cant see my Toast messages in new Thread(new Runnable()

我在连接和断开连接后显示消息时遇到一点问题。

我正在尝试按下我的连接按钮并在新线程中建立通信以获取 toast 消息:您已连接,如果我未连接以获取 exception:You 中的 toast,则未连接。

我尝试用

替换新线程

runOnUiThread(new Runnable() /*but then It underline*/}).start(); 在我的代码末尾作为 mistake.When 我删除了启动和 运行 它无法连接到我的服务器的应用程序。

我尝试了很多解决方案,但其中 none 个 works.I 如果你能帮助我,我将不胜感激。

 @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 线程上显示 Toasts。您可以使用处理程序来执行此操作。

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() 仅适用于 Thread 对象,用于启动新线程。在这里你希望你的 ui(already 运行) 线程成为焦点:)

您应该在方法 runOnUiThread 中使用它。

例如:

activity.this.runOnUiThread(new Runnable() {
    public void run() {
        Toast.makeText(getApplicationContext(), "Message", Toast.LENGTH_SHORT).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();
        }

    }