I ve made an app that targets 19-25 API, and i use sockets to send and recieve messages. All versions of android works fine but Nougat crashes.
In my activitie i open a new thread to work with Sockets Output and Input, from that thread i update UI thread with handlers and im creating buttons with listeners which send messages (output.println("...")). Everything is ok but when my app runs on Nougat and the user press a button it crashes:
    FATAL EXCEPTION: main
                    Process: com.exmple, PID: ####
                    android.os.NetworkOnMainThreadException
                  android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
                  at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
                  ....
And it points into my Thread/button.Listener/output.println(""); line.
When im using a new thread for sending messages inside the thread then it doesnt crash but the messages are corrupted. CODE:
    public class MainActivity extends AppCompatActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
       //some code...
      }
     public void GoLobby(View view) {
       Thread thread =new Thread(new watcher(Timer,ask,......));
       thread.start();
     }
THREAD CODE:
    public class watcher implements Runnable {
     Socket socket;
     PrintWriter output;
     BufferedReader input;
     public watcher(TextView TimerRef,Spinner askRef,.....){
       //some parameters....
       //....buttons code
       Start.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View v){
            output.println("START");// <-- ERROR POINTS HERE
            //IF I PUT IT IN A NEW THREAD IS OK BUT IS TOO SLOW AND I HAVE PROBLEMS WITH WHAT I SEND
            Start.setVisibility(View.INVISIBLE);
         }
       });
     }
      //some other buttons...
     @Override
     public void run() {
        boolean connected=false;
        try{
            socket = new Socket(Host, Port);
            output = new PrintWriter(socket.getOutputStream(), true);
            input = new BufferedReader(new 
            InputStreamReader(socket.getInputStream()));
            connected=true;
            if(connected){
                 sender.init(output);
                 Log.i(TAG,"ini snd/threa");
            }
        }catch (IOException e) {
        }
        //waiting messsages and updating UI with Handlers
 
    