客户端Android应用冻结在thread.start()上

时间:2014-03-17 14:10:03

标签: android multithreading

正如我之前所说,当我尝试启动线程时,我的应用程序总是崩溃,我试图评论代码的某些部分,我几乎可以确定问题出在线程的开头

    public class MouseTestActivity extends Activity implements OnTouchListener {
        Thread mainThread;
        String ip;
        ObjectOutputStream  oos;
        Socket clientSocket;
        myRunnable runProcess;

            class myRunnable implements Runnable{
                String ip;
                ObjectOutputStream  oos;
                Socket clientSocket;
                myRunnable(String ip){
                    this.ip = ip;
                }

                public void ScriviPosizione(float x,float y){
                    try{
                        this.oos.writeUTF(x+"-"+y+"\n");

                        this.oos.flush();
                    } catch (IOException e) {

                        System.out.println("Eccezione!! "+e);
                    }
                }


                public void run()
                {
                    try {
                        TextView text = (TextView) findViewById(R.id.textView1);
                        text.setText(this.ip);
                        this.clientSocket = new Socket(this.ip, 4000);
                        System.out.println(this.ip); 
                        this.oos = new ObjectOutputStream (this.clientSocket.getOutputStream());                        
                    } catch (IOException e) {
                        System.out.println("Eccezione!! "+e);

                    }

                }
            }

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


            Intent intent = getIntent();
            this.ip = intent.getStringExtra("ip");

            this.runProcess = new myRunnable(this.ip);

            mainThread = new Thread(this.runProcess );
            setupActionBar();
            GestureOverlayView c = (GestureOverlayView) findViewById(R.id.overlayView1);
            c.setOnTouchListener(this);
            this.mainThread.start();


        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        private void setupActionBar() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                getActionBar().setDisplayHomeAsUpEnabled(true);
            }
        }

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

        public boolean onTouch(View v, MotionEvent event){          
            //this.runProcess.ScriviPosizione(event.getX(),event.getY());
            int action = event.getAction();
            if ( action == event.ACTION_MOVE){
                this.runProcess.ScriviPosizione(event.getX(), event.getY());
            }

            TextView text = (TextView) findViewById(R.id.textView1);
            text.setText("ciccio");
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:

                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

    }

有人有一些建议吗?谢谢

这是LogCat

03-17 10:03:02.860:E / AndroidRuntime(1276):致命异常:Thread-86

03-17 10:03:02.860:E / AndroidRuntime(1276):进程:com.example.clientandroid,PID:1276

03-17 10:03:02.860:E / AndroidRuntime(1276):java.lang.NullPointerException

03-17 10:03:02.860:E / AndroidRuntime(1276):at com.example.clientandroid.MouseTestActivity $ myRunnable.run(MouseTestActivity.java:54)

03-17 10:03:02.860:E / AndroidRuntime(1276):at java.lang.Thread.run(Thread.java:841)

1 个答案:

答案 0 :(得分:1)

您无法执行以下行

text.setText(this.ip);

在与UI线程不同的线程上。只有UI线程才能触摸"用户界面。由于你在Activity内,你可以使用runOnUiThread,在UI线程上执行该代码