• Android开发:Can't create handler inside thread that has not线程问题解决


    一、问题如下

      1、报错内容:Only the original thread that created a view hierarchy can touch its views.Only the original thread that created a view hierarchy can touch its views.

      2、问题分析:在Android中不允许Activity里新启动的线程访问该Activity里的UI组件,Android中的UI是线程不安全的, 所有的更新UI操作都必须要在主线程中完成,而不能在新开的子线程中操作。

      3、问题背景:

      在从Android客户端向服务器发送数据,并在服务器端处理、返回数据接受之后,要显示在客户端的界面上。

      由于网络相关的操作无法在主线程进行,只能通过新线程完成这个操作,而要显示在客户端的话,又必须要通过UI线程才能完成,因此就会报出上述错误。

      这次就是在用Toast显示服务器端返回的数据时报的这个错。

    二、问题解决

      1、若想在异步线程前执行某些UI操作,比如更新progressBar进度条等操作,这种情况适合用runOnUiThread方法。
      2、若想在线程中执行UI操作,还是要用Handler:

    new Thread(new Runnable() {
                public void run() {
                    try {
                        URL url = new URL(strUrl);
                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        //设置输入流采用字节流
                        urlConnection.setDoInput(true);
                        //设置输出流采用字节流
                        urlConnection.setDoOutput(true);
                        urlConnection.setRequestMethod("POST");
                        //设置缓存
                        urlConnection.setUseCaches(false);
                        //设置meta参数
                        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                        urlConnection.setRequestProperty("Charset", "utf-8");
    
                        //连接往服务器端发送消息
                        urlConnection.connect();
    
                        DataOutputStream dop = new DataOutputStream(urlConnection.getOutputStream());
                        dop.writeBytes("username=" + URLEncoder.encode(username, "utf-8") + "&password=" + URLEncoder.encode(password, "utf-8"));
                        dop.flush();
                        dop.close();
    
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                        String result = "";
                        String readLine = null;
                        while ((readLine = bufferedReader.readLine()) != null) {
                            result += readLine;
                        }
                        bufferedReader.close();
                        urlConnection.disconnect();
                        message=URLDecoder.decode(result, "utf-8").toString();
    
                        Handler handler = new Handler(Looper.getMainLooper());
                        handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    Gson gson=new Gson();
                                    Message m=gson.fromJson(message,Message.class);
                                    Toast.makeText(LoginActivity.this, m.getMessage(), Toast.LENGTH_LONG).show();
                                }
                            });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
  • 相关阅读:
    C# 多文本取数
    【SSH网上商城项目实战25】使用java email给用户发送邮件
    【SSH网上商城项目实战24】Struts2中如何处理多个Model请求
    【SSH网上商城项目实战23】完成在线支付功能
    【SSH网上商城项目实战22】获取银行图标以及支付页面的显示
    【SSH网上商城项目实战21】从Demo中看易宝支付的流程
    【SSH网上商城项目实战20】在线支付平台的介绍
    【SSH网上商城项目实战19】订单信息的级联入库以及页面的缓存问题
    【SSH网上商城项目实战18】过滤器实现购物登录功能的判断
    【SSH网上商城项目实战17】购物车基本功能的实现
  • 原文地址:https://www.cnblogs.com/guobin-/p/12561477.html
Copyright © 2020-2023  润新知