1

我正在编写一个简单的应用程序,用于在本地网络上的服务器和应用程序之间进行通信。编写和接收消息没有问题。但我无法在 Textview 中显示收到的消息。

为了接收传入的消息,我使用了一个(工作者)线程。当我收到一些东西时,我将消息发送到主线程中的处理程序,然后该处理程序应该在 Textview 中显示消息。
但是每次处理程序尝试显示消息时,我都会收到异常:
“只有创建视图层次结构的原始线程才能触摸它的视图。”

我搜索了许多网站,包括这个网站,以找到解决方案,但仍然无法解决问题。请帮帮我。

代码:

    public class TCPClientActivity extends Activity {
    BufferedWriter output;
    Socket s = new Socket();

    //my handler
    private Handler mHandler;
    //where I want to display the message
    private TextView lblChat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.tcpclient);

        Button btnConnect = (Button)findViewById(R.id.btnConnect);
        Button btnSend = (Button) findViewById(R.id.btnSend);
        //creating the TextView
        lblChat = (TextView) findViewById(R.id.lblChat);
        final EditText tbIP = (EditText) findViewById(R.id.tbIP);
        final EditText tbInput = (EditText) findViewById(R.id.tbInput);

        //creating the handler
        mHandler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                switch(msg.what)
                {
                    case 1:
                    {
                        lblChat.append("Received: " + msg.obj + "\r\n" );
                    }
                }
            }
        };
}

//Thread in which I receive incoming Messages from the server
    Thread communication = new Thread()
    {
        @Override
        public void run() 
        {
            String finalText = "";

            while(true) 
            {
                try
                {
                    DataInputStream inputStream = new DataInputStream(s.getInputStream());

                    int bytesRead;
                    byte[] b = new byte[4096];

                    bytesRead = inputStream.read(b,0,b.length);

                finalText = EncodingUtils.getAsciiString(b);

                //sending the message to the handler
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = finalText;

                    mHandler.handleMessage(msg);
            }

                //android.view.ViewRoot$CalledFromWrongThreadException: 
                //Only the original thread that created a view hierarchy can touch its views.
                //That's the exception I get when running the program
            catch (Exception e) 
            {
                e.printStackTrace();
            }
        }
        }
    };

//method where I connect to the server
    private void connectToServer(String ip)
    {
        try
        {
            if(!ip.equals(""))
            {
                s = new Socket(ip, 3000);
            }

            else
            {
                s = new Socket("10.0.0.143", 3000);
            }

            output = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            output.write("Android Connected");
            output.flush();

    //      new CommuTask().execute();

            //starting the thread
            communication.start();
    //      s.close();
        }

        catch(UnknownHostException e)
        {
            e.printStackTrace();
            try {
                s.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

        catch(IOException e)
        {
            e.printStackTrace();
            try {
                s.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

    }
}
4

2 回答 2

0

您直接在处理程序上调用该方法(因此在您的工作线程上)而不是推送消息

代替:

 mHandler.handleMessage(msg);

你会想要:

 mHandler.sendMessage(msg);

参考: http: //developer.android.com/reference/android/os/Handler.html#sendMessage(android.os.Message)

于 2012-06-21T22:20:31.810 回答
0

而不是这个:

mHandler.handleMessage(msg);

用这个:

mHandler.sendMessage(msg);
于 2012-06-21T22:20:41.033 回答