pc125864418 2011-03-17 16:47
浏览 267
已采纳

我的这个程序在没有Button监听的时候没有出错 可是加上了一个Button监听就不能运行了是为什么啊?

问题在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());
        }
    }
};
  • 写回答

4条回答 默认 最新

  • 勤劳的小猿 2011-03-18 12:42
    关注

    [quote]想把if ((str1 = br.readLine()) != null)拿出来放到b1里;[/quote]
    我想这个不是放在哪的问题,你的br是全局的变量,无论放到哪个方法中就是可以的。
    但这不是原因所在,原因还在流上。
    [code="java"]
    s= new Socket(host,SERVERPORT);
    pw = new PrintWriter(s.getOutputStream(),true);
    br = new BufferedReader(new InputStreamReader(s.getInputStream()));[/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流