Android:从Socket读取时强制关闭

时间:2012-12-06 16:01:31

标签: android sockets forceclose

我正在尝试在Android中创建一个客户端。此客户端运行一个线程,该线程创建客户端套接字并启动另一个始终监听套接字以接收字符串的线程。当我从客户端向PC中运行的Java服务器发送字符串时,一切正常,但是当我从服务器向Android客户端发送字符串时,应用程序就完成了。为什么我会收到此错误?

以下是客户主要活动的代码:

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.TextView;

    public class MainActivity extends Activity {
        TextView chatHistorial;
        EditText msg;
        Socket client;
        DataInputStream in;
        DataOutputStream out;
        Boolean cerrar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Thread t = new Thread(new Runnable(){
                @Override
                public void run() {
                    try{
                        client = new Socket("192.168.1.33", 4444);
                        in = new DataInputStream(client.getInputStream());
                        out = new DataOutputStream(client.getOutputStream());
                        cerrar = false;

                        chatHistorial = (TextView) findViewById(R.id.chatHistorial);
                        msg = (EditText) findViewById(R.id.msg);

                        ThreadLectura tl = new ThreadLectura(in, cerrar, chatHistorial);
                        tl.start();
                    }
                    catch(Exception e){
                        // ...
                    }
                }
            });

            t.start();

            findViewById(R.id.enviar).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String cadena = msg.getText().toString();
                    try {
                        out.writeUTF(cadena);
                    } catch (IOException e) {
                        // ...
                    }
                    if(cadena.equals("exit"))
                        cerrar = true;
                }
            });
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            //getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }

    }

    class ThreadLectura extends Thread{
        DataInputStream in;
        String cadena;
        TextView chatHistorial;
        Boolean cerrar;

        public ThreadLectura(DataInputStream in, Boolean cerrar, TextView tv){
            this.in = in;
            this.cerrar = cerrar;
            chatHistorial = tv;
        }

        @Override
        public void run(){
            try{
                while(!cerrar){
                    cadena = in.readUTF();
                    chatHistorial.append("Has recibido: " + cadena);
                }
            }
            catch(IOException ioe){
                System.out.println("Error de entrada/salida: "+ioe.getMessage());
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

很难说没有看到你的logcat输出,但我认为这是因为你试图在你的后台线程中修改UI。 ThreadLectura中的这一行:

chatHistorial.append("Has recibido: " + cadena);

可能是个问题,因为chatHistorialTextView。您只需要在主UI线程中修改UI。