zoukankan      html  css  js  c++  java
  • Android开发中常见问题分析及解决

    最近公司有新的业务需求,需要开发一款APP,因为我开发过Android APP(我想告诉他们,那是4年前的事了,嘤嘤嘤),就把开发任务交给我了,当然也不是我一个人啦,让我组开发小组,说白了,就是让我来负责技术指导和框架搭建。我,能怎么办,只能硬着头皮写啊。确实有点儿手生了,所以中间还是遇到一些问题,下面记录一下吧。

    1,日志的收集

    日志记录是一个常见的需求,对开发和测试都很重要,有很多开源的日志工具,也可以自己开发,这里我选的是一个开源的工具。

    欢迎使用:TLog

    2,子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误

    这是一种典型的线程搞错了的问题。UI线程即主线程才能做UI相关的操作,网络线程里不能操作UI,如果要操作就需要使用Looper.prepare(), Looper.loop()了,但是如果在UI线程使用Looper.prepare()话,就会报上面的错误,app会停止。

    问题的难点在于怎么区分UI线程和网络线程,很简单,在网络请求的回调里的,就是网络线程,其他的就是UI线程。

    3,Only the original thread that created a view hierarchy can touch its views的问题

    原因:Android系统中的视图组件并不是线程安全的,如果要更新视图,必须在主线程(UI线程)中更新,不可以在子线程(网络线程,耗时的线程)中执行更新的操作。需要通过Handler去通知主线程更新UI。

    4,Cleartext HTTP traffic to xxx not permitted的问题

    原因:Google表示,为保证用户数据和设备的安全,针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的连接,因此运行 Android P 系统的安卓设备无论是接收或者发送流量,未来都不能明码传输,需要使用下一代(Transport Layer Security)传输层安全协议,而 Android Nougat 和 Oreo 则不受影响。此在Android P 使用HttpUrlConnection进行http请求会出现以下异常java.net.UnknownServiceException: CLEARTEXT communication ** not permitted by network security policy在Android P系统的设备上,如果应用使用的是非加密的明文流量的http网络请求,则会导致该应用无法进行网络请求,https则不会受影响,同样地,如果应用嵌套了webview,webview也只能使用https请求。

    解决方法:在AndroidManifest.xml文件中配置

    <application

    android:name=".MesApplication"

    android:allowBackup="true"

    android:icon="@drawable/logo"

    android:label="@string/app_name"

    android:roundIcon="@drawable/logo"

    android:supportsRtl="true"

    android:theme="@style/AppTheme"

    android:usesCleartextTraffic="true">

    </application>

    码字不易,如果觉得有帮助,一定要给我点赞哟~~

    不然信不信我砸了你家灯,半夜偷亲你 ( ̄ε  ̄) !!!

  • 相关阅读:
    Leetcode Substring with Concatenation of All Words
    Leetcode Divide Two Integers
    Leetcode Edit Distance
    Leetcode Longest Palindromic Substring
    Leetcode Longest Substring Without Repeating Characters
    Leetcode 4Sum
    Leetcode 3Sum Closest
    Leetcode 3Sum
    Leetcode Candy
    Leetcode jump Game II
  • 原文地址:https://www.cnblogs.com/tonyccc/p/11470093.html
Copyright © 2011-2022 走看看