学习《第一行代码》的RecyclerView时遇到的问题

安装软件后进去就闪退,log如下:

019-12-25 17:47:07.434 28887-28887/com.example.recyclerviewtest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.recyclerviewtest, PID: 28887
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.recyclerviewtest/com.example.recyclerviewtest.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class androidx.support.v7.widget.RecyclerView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class androidx.support.v7.widget.RecyclerView
     Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class androidx.support.v7.widget.RecyclerView

没有头绪,我又检查了一下发现app/build.gradle中implementation 'com.android.support:recyclerview-v7:28.1.0’下面有波浪线,说28版本是为Android pie及以下版本使用的,也对,我用的已经是Android X了,于是我把版本改到了29.1.0

这回波浪线没了,但是还是闪退,看来不怨这个。

gradle.properties中有下面两行代码:

android.useAndroidX=true
android.enableJetifier=true

查了一下,第一行表示当前项目是否启用androidx,第二行代码表示是否将依赖包迁移至androidx。
因此新建布局时需要以androidx为路径的开头,修改布局代码如下:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

再次运行就可以了。

你可能感兴趣的:(《第一行代码》,Android)