zoukankan      html  css  js  c++  java
  • Can't create handler inside thread that has not called Looper.prepare() 解决办法

    在开发的过程当中,遇到了想在一个线程中弹出一个提示窗 new AlertDialog.Builder(
               context),但是就出现了一个问题。

    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

    分析了一下原因,应该是不能在线程中操作UI界面。

    然后我就就在线程里面新开一个线程,再配上hangler通信 但是还是有错误,最后查看了handler才知道是没有getMainLooper()。

    因为在UI主线程之外是无法对UI组件进行控制的。因为你必须在新线程任务完成之后利用各种方法先UI主线程发送消息通知任务完成从而来显示各种提示消息。

    最后变成这样,搞掂了

    new Thread() {
                                @Override
                                public void run() {
                                    Message msg = new Message();
                                    msg.what = 0;
                                    handler.sendMessage(msg);
                                }
                            }.start();
    
                            handler = new Handler(context.getMainLooper()) {
    
                                @Override
                                public void handleMessage(Message msg) {
                                    if (0 == msg.what) {
                                        new AlertDialog.Builder(
                                                context)
                                                .setTitle("提示")
                                                .setIcon(R.drawable.icon)
                                                .setMessage("确定要回到打乱状态吗!!")
                                                .setPositiveButton(
                                                        "确定",
                                                        new DialogInterface.OnClickListener() {
    
                                                            public void onClick(
                                                                    DialogInterface arg0,
                                                                    int arg1) {
                                                                rBlockGroup
                                                                        .setmap(replayStart);
                                                                gamestart = GAMESTATR_REPLAY;
                                                                // replayCount = 0;
                                                                // repalyCurrentStep =
                                                                // 0;
                                                                replayState = GAMESTATR_RUN;
                                                            }
                                                        })
                                                .setNegativeButton(
                                                        "取消",
                                                        new DialogInterface.OnClickListener() {
    
                                                            @Override
                                                            public void onClick(
                                                                    DialogInterface arg0,
                                                                    int arg1) {
                                                            }
                                                        }).create().show();
                                    }
                                    
                                }
                            };
  • 相关阅读:
    LeetCode题解(14)--Longest Common Prefix
    LeetCode题解(12)--Integer to Roman
    LeetCode题解(13)--Roman to Integer
    LeetCode题解(9)--Palindrome Number
    LeetCode题解(8)--String to Integer (atoi)
    LeetCode题解(7)--Reverse Integer
    LeetCode题解(6)--ZigZag Conversion
    从并发和索引说说innodb和myisam的区别
    thrift基本概念和实例
    fastcgi+lighttpd+c语言 实现搜索输入提示
  • 原文地址:https://www.cnblogs.com/mczha/p/3655875.html
Copyright © 2011-2022 走看看