天天看点

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

今天记录一下写安卓的时候遇到的些问题吧,都是比较常见实用的

        • 1、错误:java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        • 2、简单的okhttp的Get请求。Json转数组
          • 总结:
  • 博主并不是专业的安卓开发,所以遇到的一些问题也可能比较低级,解决方案也并非是最佳。仅供参考哈!

1、错误:java.lang.RuntimeException: Can’t create handler inside thread that has not called Looper.prepare()

  • 其实这个错误也是比较常见,也是比较基础的,个人理解是说在线程里面不能直接创建新对象或者调用外部方法。或者是传参出去。(我的目的就是传参,所以我觉得是不让我传参数出去,哈哈),我用的是okhttp框架,所以GET和POST请求都是写在线程里面的,但是返回的数据怎么从线程里面取出来呢,直接调用是不行的,就是我下面的错。
java.lang.RuntimeException: Can‘t create handler inside thread that has not called Looper.prepare()

解决方案:

  • 新建一个Handler方法。这可不是一般的方法!具体大家也已自行百度看看。这方法还挺实用的。可以用于线程之间的数据交互。这也是我所要的。Handler就相当于一个中介或者邮递员,把A线程所需的数据从B线程那里带过来。
  • 先声明一个全局的Handler,在线程里面用Message把需要的数据包起来,然后交给快递员Handler。然后在线程的旁边修一个邮筒(处理数据的方法),接收并处理数据。list是我在后台取的json转的数组----我需要的数据!
//这个放在线程里面
                    Message msg = new Message();
                    msg.obj = list;
                    handler.sendMessage(msg);
           
//这个可放在线程外面,线程所在的方法里面也行。
			 handler = new Handler() {
		            @Override
		            public void handleMessage(Message msg) {
		                super.handleMessage(msg);
		                String[] list = (String[]) msg.obj;   //实例化对接收数据
		                showSingleAlertDialog(list);      //自定义的方法,真正需要参数的地方
		            }
		        };
           
  • java.lang.RuntimeException: Can‘t create handler inside thread that has not called Looper.prepare()
    java.lang.RuntimeException: Can‘t create handler inside thread that has not called Looper.prepare()

    刚博主还看到了一篇相关博客,大家也可参考一下!眼睛痛,还没尝试,没来得及看。等明天再看吧。

    链接:https://blog.csdn.net/dxt8305/article/details/84067538

  • 然后参数就传出来了!看看最后的成果吧!点击按钮,后端获取名单弹框显示列表,一个简单的Demo。哈哈,现在最大的成就感就是每次代码成功运行,功能慢慢实现吧。
    java.lang.RuntimeException: Can‘t create handler inside thread that has not called Looper.prepare()

2、简单的okhttp的Get请求。Json转数组

  • 1、新建一个get请求。可以单独新建一个类来装请求。我的是ache
//返回的类型啥的自己定
static String[] get() {
        Log.d("success", "get start !");
         //还有这句也是必须的,requestURL是我需要访问的后端接口ip+接口(例如:http://192.168.x.x:8080/xx/xx)
        Request request = new Request.Builder().url(requestURL).build();   
        try {
           //主要是这句获取Json返回,这是Get请求的
            Response response = okHttpClient.newCall(request).execute(); 
            //然后就是操作返回的数据了
            if (response.isSuccessful()) {
               // 我这是把返回的json{"张三",“莉莉丝”,“王二狗”}转换成一个String9[]数组,下面根据自己需求操作即可
                 String result = response.body().string();
                JSONArray arr = JSONArray.parseArray(result);    
                Log.d("success", arr.toString());
                return arr.toArray(new String[0]);
            }
            Log.d("fail", "返回参数错误 !");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("fail", "返回参数为空 !");
        return null;
    }
           
  • 2、在MainActivity里面新建一个线程调用请求。即可
new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String[] list = cache.get();     //关键焦点在这里,直接建一个对象来接返回的数据
                    Message msg = new Message();
                    msg.obj = list;
                    handler.sendMessage(msg);
                    Log.d("success", "run: " + list);
                } catch (Exception e) {
                    Log.d("failure", "run: " + e.toString());
                }

            }
        }).start();
           
  • 当然返回的数据需要从线程里面取出来,就需要回到第一个问题里面去了!
总结:
  • 博主还是个刚入门的小菜鸟,而且不是专业安卓开发,遇到的许多问题都是自己摸索的。了解程度还不是很深。也许我的方法并非正确解。但是能实现功能。如果有更准确的方法也请留言告诉一下博主。知识浅薄,如有错误,望指正!谢谢!
  • 致在成长路上一起努力的我们!