Wednesday, February 5, 2014

android.view.ViewRoot $ CalledFromWrong how to resolve this anomaly. .


            
Is to use socket programming, and has been able to connect to the server, and the server is mainly sent a message to display on a TextView, began to write a single pass in a class directly change the activity inside TextView, not found, appeared above exception, after Baidu said to use handler, did as above with the handler, but found or not, people say can not pass activity, activity should operate are placed inside, well, then change (in fact, nothing is moved a place), but later found or not, still the exception, I posted the following code, we can give a look ah. .
 package com.example.chatphone; 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
private TextView disText = null;
private EditText input = null;
private Button btnSend = null;
private MyHandler myHandler = null;

private BufferedReader BR = null;
private PrintWriter PW = null;
private Socket s = null;
private boolean isConnectioned = false;
private Thread thRecv = null;

@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
myHandler = new MyHandler ();
disText = (TextView) findViewById (R.id.disText);
input = (EditText) findViewById (R.id.input);
btnSend = (Button) findViewById (R.id.btnSend);
connection ();
btnSend.setOnClickListener (new OnClickListener () {

@ Override
public void onClick (View v) {
send ("hello");
}
});
}

@ Override
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater () inflate (R.menu.main, menu);.
return true;
}

/ **
* Define handle remind ui update
* /
class MyHandler extends Handler {

@ Override
public void handleMessage (Message msg) {
super.handleMessage (msg);
Bundle b = msg.getData ();
String msgStr = b.getString ("msg");
disText.setText (msgStr);
}
}

private class Recv implements Runnable {

@ Override
public void run () {
while (true) {
try {
System.out.println ("run");
String msgStr = BR.readLine ();
Message msg = new Message ();
Bundle b = new Bundle ();
b.putString ("msg", msgStr);
msg.setData (b);
System.out.println (msg + "- >>");
MainActivity.this.myHandler.handleMessage (msg);
} Catch (IOException e) {
e.printStackTrace ();
}
}
}
}

private void connection () {
try {
isConnectioned = true;
s = new Socket ("169.254.66.14", 8888);
BR = new BufferedReader (new InputStreamReader (s.getInputStream ()));
PW = new PrintWriter (s.getOutputStream ());
thRecv = new Thread (new Recv ());
thRecv.start ();
} Catch (UnknownHostException e) {
e.printStackTrace ();
} Catch (IOException e) {
e.printStackTrace ();
}
}

/ **
* As the server sends a message
*
* @ Param msg
* Message to send
* /
private void send (String msg) {
PW.println (msg);
PW.flush ();
}
}

Reply:
myHandler.handleMessage (msg); not so used,
Instead myHandler.sendMessage (msg);
Reply:
cited a floor ameyume reply:
myHandler.handleMessage (msg); not so used,
Instead myHandler.sendMessage (msg);
Blessings, thank you, a success. .

No comments:

Post a Comment