0

I have a problem with the connection tcp in android. The exception is this:

03-17 17:39:29.374    1089-1089/com.example.dhorka.client E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.dhorka.client, PID: 1089
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4007)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
            at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
            at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
            at libcore.io.IoBridge.connect(IoBridge.java:122)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
            at java.net.Socket.startupSocket(Socket.java:590)
            at java.net.Socket.<init>(Socket.java:226)
            at com.example.albert.client.ConnectionManagement.run(ConnectionManagement.java:39)
            at java.lang.Thread.run(Thread.java:818)
            at com.example.albert.client.MainActivity.click(MainActivity.java:43)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4002)
            at android.view.View.performClick(View.java:4756)
            at android.view.View$PerformClick.run(View.java:19749)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-17 17:39:31.479    1089-1089/com.example.dhorka.client I/Process﹕ Sending signal. PID: 1089 SIG: 9

I dont know what is the problem. The code I'm using is:

public class ConnectionManagement implements Runnable  {
private static ConnectionManagement instance =  new ConnectionManagement();
private String ip;
private int port;
private Socket connection;
private PrintWriter printWriter;

private ConnectionManagement(){
    this.ip = "127.0.0.1";
    this.port = 700;
}

public static ConnectionManagement getInstance(){
    return instance;
}

public void setIp(String ip){
    this.ip=ip;
}

public void setPort(int port){
    this.port=port;
}

public void run(){
        try {
            connection = new Socket(InetAddress.getByName(this.ip), this.port);
            PrintWriter printWriter = new PrintWriter(connection.getOutputStream(), true);
        } catch (IOException e) {
            e.printStackTrace();
        }

}
public void sendMessage(String message){
    printWriter.write(message);
}

public void disconnect(){
    try {

        printWriter.flush();
        printWriter.close();
        connection.close();

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

public boolean isClosed(){
        return connection.isClosed();
}

When I call this class in the main activity I'm doing this:

 case R.id.connect:
                EditText editText = (EditText) findViewById(R.id.ip);
                String ip = editText.getText().toString();
                connection.setIp(ip);
                new Thread(connection).run();
            break;

I don't know what are I doing wrong.

Thanks for you attention.

Dhorka
  • 686
  • 1
  • 6
  • 17

1 Answers1

1

You try to start thread in wrong way. If you want to start new Thread you should use start() method. Now you just call run() method in the main thread and you have NetworkOnMainThreadException.

Change:

new Thread(connection).run();

to

new Thread(connection).start();

in R.id.connect case.

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45