无法从线程中创建的while循环中退出

时间:2013-02-14 10:24:55

标签: android thread-sleep

我试图在过去的3天内完成它,但仍然没有成功。 在我的Android应用程序中,我有两个活动。我点击按钮从1开始活动2。在活动二的onCreate事件中,我创建了一个持续侦听套接字数据的线程。为此,我使用了while循环。现在我能够持续接收数据。但是我无法退出while循环,因此每当我尝试回到Activity 1时,它都会给我'强制关闭'错误。我正在使用以下逻辑来打破循环_

全局设置int j = 1, 在活动2的OnCreate事件中启动新线程 把while循环放在run()as_ while(j == 1){代码监听端口数据} 在活动2的OnStop事件中设置j = 0

public class MainActivity extends Activity {

//DATA RECEIVE
String mClientMsg = "",temp="";
Thread myCommsThread = null;
protected static final int MSG_ID = 0x1337;
public static final int SERVERPORT = 2000;
TextView tv=null;
BufferedReader input;
volatile int j=1; 

       //Message handler
   Handler myUpdateHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case MSG_ID:

                tv.setText(RecString);  

                break;
            default:
                break;
            }
            super.handleMessage(msg);
        }
       };




@Override
   protected void onStop() {
    super.onStop();
   j=0;

            try {
            MainPageActivity.socket.close();


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();


        }

   }



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

        this.myCommsThread = new Thread(new CommsThread());
        this.myCommsThread.start();

    }
    catch (Exception e1)
    {
         Toast.makeText(this,e1.toString(), Toast.LENGTH_SHORT).show();


    }



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}





  class CommsThread implements Runnable {
        public void run() {
            try{            
            while (j==1) {

                Message m = new Message();
                m.what = MSG_ID;

                try {

                    input = new BufferedReader(new InputStreamReader(MainPageActivity.socket.getInputStream()));
                    String st = null;
                    st = input.readLine();
                    mClientMsg = st;
                    myUpdateHandler.sendMessage(m);

                } 
            catch (IOException e) {
                    e.printStackTrace();

                }

            }


            }
            catch(Exception e){
           tv.setText(e.toString());
            }
        }
        }

}

  

02-15 13:44:02.289:I / ApplicationPackageManager(2796):cscCountry不是德语:INS   02-15 13:44:02.390:D / dalvikvm(2796):GC_EXTERNAL_ALLOC释放26K,48%免费2801K / 5379K,外部758K / 1491K,暂停89ms   02-15 13:44:06.625:I / ApplicationPackageManager(2796):cscCountry不是德语:INS   02-15 13:44:06.687:D / dalvikvm(2796):GC_EXTERNAL_ALLOC释放39K,47%自由2874K / 5379K,外部1116K / 1840K,暂停51ms   02-15 13:44:10.499:W / KeyCharacterMap(2796):无法打开keycharmap文件   02-15 13:44:10.499:W / KeyCharacterMap(2796):加载keycharmap文件'/system/usr/keychars/sec_touchscreen_.kcm.bin'时出错。 hw.keyboards.65538.devname ='sec_touchscreen'   02-15 13:44:10.499:W / KeyCharacterMap(2796):使用默认键映射:/system/usr/keychars/qwerty.kcm.bin   02-15 13:44:10.742:I / ApplicationPackageManager(2796):cscCountry不是德语:INS   02-15 13:44:10.898:W / dalvikvm(2796):threadid = 10:线程退出未捕获异常(组= 0x40018578)   02-15 13:44:10.898:W / dalvikvm(2796):threadid = 9:线程退出未捕获异常(组= 0x40018578)   02-15 13:44:10.898:E / AndroidRuntime(2796):致命异常:线程-11   02-15 13:44:10.898:E / AndroidRuntime(2796):java.lang.NullPointerException   02-15 13:44:10.898:E / AndroidRuntime(2796):at com.example.wifigui.MainActivity $ CommsThread.run(MainActivity.java:326)   02-15 13:44:10.898:E / AndroidRuntime(2796):at java.lang.Thread.run(Thread.java:1019)

0 个答案:

没有答案