[技術博客]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