alexca
alexca

Reputation: 57

Handler.post() thread, is crashing application

I work on an app, which have two activities. MainActivity have to editext in order to send an IP address and Port with Intent, to the second Activity2.

The problem I have is that when I use Handler.post() in order to update TextView in the UI thread, app crashes. Without handler thread app running correctly. I think that my code is correct but I cannot understand the reason for this problem.

public class Activity2 extends Activity {

private Socket s;
private OutputStream out = null;
private PrintWriter w = null;
private Handler handler = new Handler();
private TextView textView1;
private String tag = "ALEX";
private static String IP;
private static int port;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity2);

    Bundle extras = getIntent().getExtras(); 
    if (extras != null) {
        IP = extras.getString("IP");
        String port2 = extras.getString("PORT");
        port = Integer.parseInt(port2);
        // Log.v("ip",ip);
        // Log.v("port",port);
    }

Runnable runnable = new Runnable() {
        public void run() {

            synchronized (this) {
                try {
                    s = new Socket(IP, port);
                    out = s.getOutputStream();
                    w = new PrintWriter(out);
                } catch (Exception e) {
                    Log.v("error socket", "Alex soc");
                    e.printStackTrace();
                }
            }

            **handler.post(new Runnable() {
                @Override
                public void run() {
                    synchronized (this) {
                        try {
                            Thread.sleep(1000);
                            if (s.isConnected)
                                textView1.setText("connected...");
                            // textView1.setText("not connected...");
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            Log.v("error handler", "handler Alex");
                            e.printStackTrace();
                        }
                    }
                }
            });**

        }
    };
    Thread mythread = new Thread(runnable);
    mythread.start();

Upvotes: 2

Views: 2274

Answers (1)

Mr.Me
Mr.Me

Reputation: 9276

The problem is that you are creating your handler object once your activity object is created.

Handlers should be created after the Looper is prepared.

so your code should be something like this:

private Handler handler;
private TextView textView1;
private String tag = "ALEX";
private static String IP;
private static int port;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity2);
  handler   = new Handler();

Upvotes: 3

Related Questions