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

第一次遇见这个问题:

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值