0

In my app when I use dataoutputstream without button it works but when I use it with button it crashes!
I've needed permissions in manifest. At first dataoutputstream works but when I use button it crashes.

private DataOutputStream outputStream;

final Thread thread = new Thread(new Runnable() {
  @Override
  public void run() {

    try {

        socket = new Socket(ip,9000);

      log("Connected!");


        outputStream = new DataOutputStream(socket.getOutputStream());

        outputStream.write("hi server\n".getBytes());


    }
    catch (IOException e) {
      log("Not Connected!");
    }
  }
});
btn_send.setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View v) {
    if (outputStream == null) {
      return;
    }
      String message = txt_user.getText().toString() + "\n";
    try {
      outputStream.write(message.getBytes());
    } catch (IOException e) {
      e.printStackTrace();
    }

  }
});


  thread.start();

}

Eugene Primako
  • 2,767
  • 9
  • 26
  • 35

0 Answers0