[技术博客]Android 开发 Bug Log

[技术博客] Android 开发 Bug Log

大大小小的bug,聪明的愚蠢的都有, 持续记录中......java

  1. java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).android

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.termux/com.termux.app.TermuxActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton

    控件FloatingActionButton 所在的Activity的theme是自定义的Theme, 而这个控件要求app theme为Theme.AppCompat.在xml中修改对应的Activity的android:theme="@style/Theme.AppCompat"shell

  2. android activity is not an enclosing classbash

    这一般是个很尴尬(新手易犯)的错误, class 写成this了,也可能真的是内部类的问题.闭包

    Intent intent  = new Intent(MainActivity.this, xxxActivity.this);
    //改为
    Intent intent  = new Intent(MainActivity.this, TermuxActivity.class);
  3. Attempt to invoke virtual method架构

    NullPointerException:Attempt to invoke virtual method...

    多半是空指针问题, 有的是控件找不到, 有的是资源找不到, 有的是各类属性方法错误,可是没有报出这些个bug,而是产生了一个NullPointer, 而后在NullPointer上调用方法,就会报这个bug。很容易定位。app

  4. 兼容性Bug: AndroidX布局

    We hope the division between android.* and androidx.* makes it more obvious which APIs are bundled with the platform, and which are static libraries for app developers that work across different versions of Android.gradle

    从 API 28(Android 9.0,Pie)开始,Google 推荐开发者从原来的各类支持库转移到一个新版本的名为 AndroidX 的支持库。它相比老支持库有着无需操心版本控制、实时更新的优势。原有的支持库将被保留而且能够继续使用,但接下来全部新的支持都将发布在 AndroidX 上。ui

    因为咱们的项目使用了多个开源项目的代码做为基础,兼容性问题及其明显,有各类不一样版本的com.android.support 依赖库,所以咱们决定统一使用AndroidX来解决兼容性难题。

    AndroidStudio 迁移项目到AndroidX: 点击 Android Studio 的 Refactor > Migrate to AndroidX...

    这能解决大部分的支持库兼容问题,但不是所有,对于代码中仍然存在的各类com.android.support.XXX能够在https://developer.android.google.cn/jetpack/androidx/migrate, 找到对应包名的改动,而后在工程中对应更改就好了。

    AndroidX:https://developer.android.google.cn/jetpack/androidx

  5. Android 添加第三方lib和jar

    在app的libs目录中拷贝所要假如的.so文件或者.jar文件

    在app模块的build.gradle中的android闭包中加入:

    android{
        sourceSets{
            main{
                jniLibs.srcDirs = ['libs']
            }
        }
    }

    点击gradle sync同步整个工程便可。Note: 模拟器的架构一般是x86, 而一般的android设备为aarch64, 有可能.so文件在模拟器可用而在设备上不能使用。

  6. JNI 与 NDK

    java本地接口为JNK,android的本地接口为NDK, 主要用于调用本地C/C++接口。

    配置NDK:

    SDK Manager ➡️SDK tools, 勾选NDK和LLDB,apply & OK

    注意NDK的版本,容易所以报编译上的bug.

    termux会使用本地C接口开进程来运行bash或者别的shell。绕过android的层层封装。

  7. View 填充bug

    自定义的View很容易遇到这个bug:

    android.view.InflateException: Binary XML file line #0: Error inflating class

    在对应的xml文件中定位错误的元素。

    可能的缘由有不少:

    1. 在自定义的View中使用了高版本的SDK中的方法,在低版本SDK中运行时出现android.view.InflateException。须要进行SDK版本的断定,改写相关操做。
    2. 自定View在布局中出现错误的书写
    3. 内部自定义view出现android.view.InflateException的状况

    refer