android -- Can't create handler inside thread that has not called Looper.prepare()

来源:互联网 发布:钻戒哪个牌子好 知乎 编辑:程序博客网 时间:2024/06/10 08:12

第一次遇见这个问题:

Can’t create handler inside thread that has not called Looper.prepare()
百度翻译下:

无法创建程序的线程没有调用prepare()活套。

//什么乱七八糟的。。。。

我的报错的程序是这样的:

1、主MainActivity:

主要目的:主程序开启后3s,调用对时方法

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         new Thread(new Runnable(){                @Override                public void run() {                    try {                        Thread.sleep(3000);                    } catch (InterruptedException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                    Select_Http sh=new Select_Http();                    sh.getTime_Http();//校对服务器时间                 }          }).start();    }

2、Select_Http类:

public void getTime_Http(){        sh=new SyncHttp();        new Thread(new Runnable(){            @Override            public void run() {                System.out.println("获取时间");                String response="";                try {                    response=sh.httpGet(Url_GetTime, "");                               Message message = new Message();                    Bundle bundle = new Bundle();                    bundle.putString("msg", response);                    message.setData(bundle);                    handler_time.sendMessage(message);//错误在这里;因为这样就调用到主程序啦                } catch (Exception e) {                    e.printStackTrace();                }            }        }).start();    }    ```3、handler_time

private Handler handler_time = new Handler() {

        public void handleMessage(Message msg) {      //处理数据。。。。        };    }; 
修改下3、handler_time

private Handler handler_time = new Handler(MainActivity.mainActvity.getMainLooper()) {
}
“`
加上这个(MainActivity.mainActvity.getMainLooper())

0 0
原创粉丝点击