zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    [笔记]Oracle遇到的问题及解决的办法
    [转载] linux三款好用网络监控软件(bwmng 、iftop、iptraf)
    IE无法打开internet站点已终止操作的解决办法 (转)
    怎样去掉桌面图标和字的蓝色阴影
    ASP.NET下载文件(转载)
    获取iframe内容IE下的延时 (转载)
    文件上传入数据库&从数据库中下载文件(转载)
    asp.net 4.0 A potentially dangerous Request.Form value was detected fr(转载)
    location.search
    IE6下的CSS BUG枚举 (转)
  • 原文地址:https://www.cnblogs.com/guobin-/p/12561477.html
Copyright © 2011-2022 走看看