[技术博客]Android 开发 Bug Log

[技术博客] Android 开发 Bug Log

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

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

    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"

  2. android activity is not an enclosing class

    这通常是个很尴尬(新手易犯)的错误, 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。很容易定位。

  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.

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

    由于我们的项目使用了多个开源项目的代码作为基础,兼容性问题及其明显,有各种不同版本的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

猜你喜欢

转载自www.cnblogs.com/bingduoduo/p/10919909.html