Android 报错:can't create handler inside thread that has not called looper.prepare() toast

错误原因:在线程中更新UI!

不要在子线程中更新UI!不要在子线程中更新UI!不要在子线程中更新UI!  重要的事情说三遍!!!

错误代码如下:

public class SendThread implements Runnable{

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try{

                sock.getOutputStream().write(new SendOver(P[6],P[7],P[8],P[9]).getbuf());  
                Toast.makeText(getApplicationContext(), "I am sending", Toast.LENGTH_SHORT).show();  //此句应该删掉
                sock.close(); 
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        
    }

应该要把Toast 这个函数放在Handler中解决。

posted @ 2017-02-23 17:15  风吹来的砂  阅读(201)  评论(0编辑  收藏  举报