现在一运行就崩溃
我把代码贴上吧- package lcgl.com;

  • - import java.io.IOException;
  • import java.util.Timer;
  • import java.util.TimerTask;
  • - import android.app.Activity;
  • import android.content.Intent;
  • import android.net.Uri;
  • import android.os.Bundle;
  • import android.os.Handler;
  • import android.os.Message;
  • import android.view.View;
  • import android.view.View.OnClickListener;
  • import android.widget.Button;
  • import android.widget.TextView;
  • import android.widget.Toast;
  • - public class LcglActivity extends Activity {
  • /** Called when the activity is first created. */
  • - String msg;
  • String msgrec;
  • //byte msgbyte[];
  • Button b1;//=(Button)findViewById(R.id.button1);
  • Button b2;//=(Button)findViewById(R.id.button2);
  • Button b3;//=(Button)findViewById(R.id.button3);
  • Button b4;
  • Button b5;
  • Button b6;
  • Button b7;
  • Button b8,b9,b10,b11,b12,b13,b14,b15;
  • TextView t1,t2;
  • boolean **=false;//防止冲突运行死锁
  • - Message message = new Message();//定义一个消息
  • Handler handler=new Handler(){//定义已handler处理线程事物,循环执行或多个同时执行的线程事物
  • @Override
  • public void handleMessage(Message m) {
  • // TODO Auto-generated method stub
  • super.handleMessage(m);
  • switch (m.arg1)
  • {
  • case 1:
  • //msg="01GIO";
  • runnrec("01GIO");//获取两个开关的状态
  • //tmp=msgrec;
  • t2.setText("开关的状态:"+msgrec);
  • System.out.println("开关的状态:"+msgrec);
  • break;
  • }
  • }};
  • - 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);
  • b5=(Button)findViewById(R.id.button5);
  • b6=(Button)findViewById(R.id.button6);
  • b7=(Button)findViewById(R.id.button7);
  • b8=(Button)findViewById(R.id.button8);
  • b9=(Button)findViewById(R.id.button9);
  • b10=(Button)findViewById(R.id.button10);
  • b11=(Button)findViewById(R.id.button11);
  • b12=(Button)findViewById(R.id.button12);
  • b13=(Button)findViewById(R.id.button13);
  • b14=(Button)findViewById(R.id.button14);
  • b15=(Button)findViewById(R.id.button15);
  • t1=(TextView)findViewById(R.id.t2);
  • t2=(TextView)findViewById(R.id.textView1);
  • - b1.setOnClickListener(li);
  • b2.setOnClickListener(li);
  • b3.setOnClickListener(li);
  • b4.setOnClickListener(li);
  • b5.setOnClickListener(li);
  • b6.setOnClickListener(li);
  • b7.setOnClickListener(li);
  • b8.setOnClickListener(li);
  • b9.setOnClickListener(li);
  • b10.setOnClickListener(li);
  • b11.setOnClickListener(li);
  • b12.setOnClickListener(li);
  • b13.setOnClickListener(li);
  • b14.setOnClickListener(li);
  • b15.setOnClickListener(li);
  • - new Thread(){
  • public void run() {
  • while(true)
  • {
  • message.arg1=1;//给handler一个信号去处理
  • handler.sendMessage(message);//送出信号
  • - try {//延时5秒,即每隔5秒运行一次
  • Thread.sleep(5000);
  • } catch (InterruptedException e) {
  • // TODO Auto-generated catch block
  • e.printStackTrace();
  • }
  • }
  • - };}.start();
  • - }
  • - - public void runns(String tmpmsg) {//发送命令,发送字符串,
  • mysock_char socket=new mysock_char("192.168.1.57",6510);
  • if (socket.isconnect())
  • {
  • socket.sendmsg(tmpmsg);
  • t1.setText(tmpmsg);
  • try {//关闭socket之前,延迟一下,等socket自行完毕
  • Thread.sleep(100);
  • } catch (InterruptedException e) {
  • // TODO Auto-generated catch block
  • e.printStackTrace();
  • }
  • socket.close();
  • }
  • else
  • {
  • Toast.makeText(LcglActivity.this, "连接失败", Toast.LENGTH_LONG);
  • }
  • }
  • /*
  • public void runnsbyte(){////发送命令,发送byte类型
  • - mysocket socket=new mysocket("192.168.1.57",6510);
  • if (socket.isconnect())
  • {
  • try {
  • socket.sendmsg(msgbyte);
  • //t1.setText(msg);
  • try {
  • Thread.sleep(100);
  • } catch (InterruptedException e) {
  • // TODO Auto-generated catch block
  • e.printStackTrace();
  • }
  • socket.close();
  • } catch (IOException e1) {
  • // TODO Auto-generated catch block
  • e1.printStackTrace();
  • }
  • }
  • else
  • {
  • Toast.makeText(LcglActivity.this, "连接失败", Toast.LENGTH_LONG);
  • }
  • }
  • */
  • - - 复制代码