0

我的客户端类在我的日志跟踪但我的应用程序下。最终以错误“客户端已停止”停止,我创建了带有数据报包的数据报套接字以在接收应用程序上发送。并且我已经指定了接收设备的 IP。请确定我的错误在哪里,在此先感谢。

   public class MainActivity extends Activity {

private DatagramSocket socket;
String str;
private static final int SERVERPORT = 6000;
private static final String SERVER_IP = "192.168.1.101";

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

    new Thread(new ClientThread()).start();
}

public void onClick(View view) {
    try {
        EditText et = (EditText) findViewById(R.id.EditText01);
         str = et.getText().toString();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

class ClientThread implements Runnable {

    @Override
    public void run() {

        try {
        socket = new DatagramSocket(SERVERPORT);
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
            DatagramPacket packet = new DatagramPacket(str.getBytes(),   str.length(),serverAddr,SERVERPORT);                         
                socket.send(packet);
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

    }

}}

我的日志猫是:

   08-25 16:27:22.657: W/dalvikvm(555): threadid=11: thread exiting with uncaught      exception (group=0x409961f8)
  08-25 16:27:22.677: E/AndroidRuntime(555): FATAL EXCEPTION: Thread-75
  08-25 16:27:22.677: E/AndroidRuntime(555): java.lang.NullPointerException
  08-25 16:27:22.677: E/AndroidRuntime(555):    at   com.example.client.MainActivity$ClientThread.run(MainActivity.java:51)
 08-25 16:27:22.677: E/AndroidRuntime(555):     at    java.lang.Thread.run(Thread.java:856)
 08-25 16:28:04.288: I/Process(555): Sending signal. PID: 555 SIG: 9
4

0 回答 0