问题在b1上 去掉b1的监听就能运行,可是加上监听就不能运行了。。谁能帮我看看~
public static final String host = "10.10.9.132";
public static final int SERVERPORT = 10000;
Thread th = null;
Socket s = null;
BufferedReader br = null;
PrintWriter pw = null;
private EditText et1 = null;
private EditText et2 = null;
String str1 = "";
private Button b1 = null;
private Button b2 = null;
private Button b3 = null;
private Button b4 = null;
//private Runnable mRunnable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b4 = (Button)findViewById(R.id.button4);
et1 = (EditText)findViewById(R.id.editText1);
et2 = (EditText)findViewById(R.id.editText2);
b1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
try {
s= new Socket(host,SERVERPORT);
pw = new PrintWriter(s.getOutputStream(),true);
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
//发送消息。。
b2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
try{
//取得编辑框中输入的消息。。
String str = et2.getText().toString() +"/n";
et2.setText("");
//发送给服务器。。
pw.print(str);
pw.flush();
}catch (Exception e){
Log.e(DEBUG_TAG,e.toString());
}
}
});
b3.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated methd stub
String ss = "button33333";
pw.print(ss);
pw.flush();
}
});
th = new Thread(mRunnable);
th.start();
}
//监听服务器发来的消息。。
private Runnable mRunnable = new Runnable()
{
public void run()
{
while (true){
try {
if ((str1 = br.readLine()) != null)
{//消息换行。。
str1 += "/n";
mHandler.sendMessage(mHandler.obtainMessage());
}//发送消息。。
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Handler mHandler =new Handler(){
public void handleMessage(Message msg){
//刷新
super.handleMessage(msg);
try
{
//将聊天记录添加进来
et1.append(str1);
}catch(Exception e){
Log.e(DEBUG_TAG,e.toString());
}
}
};