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

时间:2020-03-24
本文章向大家介绍Android开发:Can't create handler inside thread that has not线程问题解决,主要包括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();

原文地址:https://www.cnblogs.com/guobin-/p/12561477.html