我无法在新线程中看到我的Toast消息(new Runnable()

时间:2016-07-13 13:51:28

标签: android multithreading exception tcp toast

我在连接和disconection后显示消息时遇到一些问题。

我正在尝试按下我的连接按钮并在新线程中建立通信以获取Toast消息:您已连接,如果我没有连接以获取异常中的吐司:您没有连接。

我尝试用

替换新的Thread 我的代码末尾的

runOnUiThread(new Runnable() /*but then It underline*/}).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();
    }
   });

4 个答案:

答案 0 :(得分:3)

您必须在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();
    }
});

答案 1 :(得分:0)

您必须按如下方式使用runOnUiThread:

不要尝试在UI线程中使用SOCKET部分,它会出现异常

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(已经运行)线程重点关注:)

答案 2 :(得分:0)

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

例如:

activity.this.runOnUiThread(new Runnable() {
    public void run() {
        Toast.makeText(getApplicationContext(), "Message", Toast.LENGTH_SHORT).show();
    }
});

答案 3 :(得分:0)

考虑使用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();
        }

    }