-1

我有一个 android 套接字服务器,它只是在启动时向客户端发送“hello”。每次单击按钮时,我都需要发送“你好”。我添加了按钮的代码,在while循环中,应用程序崩溃了。请帮助(我不想显示传入的数据)

public class MainActivity extends Activity {
    TextView textView1;
    final Button b1 = (Button) findViewById(R.id.button1);

    static String SERVER_IP;
    static final int SERVER_PORT = 5000;

    Handler handler = new Handler();
    ServerSocket serverSocket;




    //---get the local IPv4 address---
    public String getLocalIpv4Address() {
        try {
            for (Enumeration<NetworkInterface> networkInterfaceEnum = NetworkInterface
                    .getNetworkInterfaces(); networkInterfaceEnum
                    .hasMoreElements();) {
                NetworkInterface networkInterface = networkInterfaceEnum
                        .nextElement();
                for (Enumeration<InetAddress> ipAddressEnum = networkInterface
                        .getInetAddresses(); ipAddressEnum.hasMoreElements();) {
                    InetAddress inetAddress = (InetAddress) ipAddressEnum
                            .nextElement();
                    // ---check that it is not a loopback address and
                    // it is IPv4---
                    if (!inetAddress.isLoopbackAddress()
                            && InetAddressUtils.isIPv4Address(inetAddress
                                    .getHostAddress())) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e("IPAddress", ex.toString());
        }
        return null;
    }

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

        //---make the TextView scrollable---
        textView1 = (TextView) findViewById(R.id.textView1);
        textView1.setMovementMethod(new ScrollingMovementMethod());
    }



    public class ServerThread implements Runnable {
        public void run() {

            try {
                if (SERVER_IP != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            textView1.setText(textView1.getText()
                                    + "Server listening on IP: " + SERVER_IP
                                    + "\n");
                        }
                    });

                    //---create an instance of the server socket---
                    serverSocket = new ServerSocket(SERVER_PORT);

                    while (true) {
                        //---wait for incoming clients---
                        Socket client = serverSocket.accept();

                        //---the above code is a blocking call;
                        // i.e. it will block until a client connects---

                        //---client has connected---
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                textView1.setText(textView1.getText()
                                        + "Connected to client." + "\n");
                            }
                        });





                        try {
                            //---get an InputStream object to read from the
                            // socket---
                            //DataInputStream dataInputStream=new DataInputStream(client.getInputStream());
                            //String line=dataInputStream.toString();
                            //textView1.setText(line);

                            final DataOutputStream dataOutputStream = new DataOutputStream(client.getOutputStream());
                            dataOutputStream.writeChars("hello");






                            /*BufferedReader br = new BufferedReader(
                                    new InputStreamReader(
                                            client.getInputStream()));

                            OutputStream outputStream = 
                                    client.getOutputStream();


                            //---read all incoming data terminated with a \n
                            // char---
                            //String line = null;
                            //while ((line = br.readLine()) != null) {
                                //final String strReceived = line;

                                //---send whatever you received back to the
                                // client---
                                //String s = line + "\n";
                                //DataOutputStream saj=new DataOutputStream();

                                //outputStream.write(s.getBytes());

                                /*handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        textView1.setText(textView1.getText()
                                                + strReceived + "\n");
                                    }
                                });*/
                                //br.close();
                            //}


                            //---client has disconnected from the server---
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    textView1.setText(textView1.getText()
                                            + "Client disconnected." + "\n");
                                }
                            });

                        } catch (Exception e) {
                            final String error = e.getLocalizedMessage();
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    textView1.setText(textView1.getText()
                                            + error);
                                }
                            });                         
                        }
                    }
                } else {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            textView1.setText(textView1.getText()
                                    + "No internet connection on device."
                                    + "\n");
                        }
                    });
                }
            } catch (Exception e) {
                final String error = e.getLocalizedMessage();
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        textView1.setText(textView1.getText() + error + "\n");
                    }
                });
            }

            handler.post(new Runnable() {
                @Override
                public void run() {
                    textView1.setText(textView1.getText() + "\n" + "Server exited"
                            + "\n");
                }
            });
        }
    }





    @Override
    protected void onStart() {
        super.onStart();
        //---get the IP address of itself---
        SERVER_IP = getLocalIpv4Address();

        //---start the server---
        Thread serverThread = new Thread(new ServerThread());
        serverThread.start();

    }




    @Override
    protected void onStop() {
        super.onStop();
        try {           
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}

4

1 回答 1

0

就像您“引入” textview 一样,您应该对按钮执行相同的操作,因为现在您正试图通过 id 但在设置布局之前找到它。这样做:

TextView textView1;
Button button1;

之后

    setContentView(R.layout.activity_main);
    //---make the TextView scrollable---
    textView1 = (TextView) findViewById(R.id.textView1);

b1 = (Button) findViewById(R.id.button1);

至少没有 logcat 也很难提供帮助

于 2013-06-03T09:44:00.860 回答