错误:将Fragment中的TabLayout放大时,java.lang.ClassNotFoundException

时间:2019-03-07 02:25:26

标签: android android-layout android-tablayout

每当Android尝试在我的Fragment中膨胀TabLayout时,我都会收到错误:java.lang.ClassNotFoundException。我正在使用Kotlin,不确定是否重要。

我的片段布局

<LinearLayout android:layout_height="match_parent"
          android:layout_width="match_parent"
          android:orientation="vertical"
          xmlns:android="http://schemas.android.com/apk/res/android">

<!-- our tablayout to display tabs  -->
<android.support.design.widget.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<!-- View pager to swipe views -->
<android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"/>

我的Gradle依赖项

def room_version = "2.1.0-alpha04"

implementation "androidx.room:room-runtime:$room_version"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
annotationProcessor "androidx.room:room-compiler:$room_version"
// For Kotlin use kapt instead of annotationProcessor

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0-alpha02'
implementation 'androidx.core:core-ktx:1.1.0-alpha04'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.2-alpha01'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.2-alpha01'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.android.support:support-compat:28.0.0'
implementation 'com.android.support:design:28.0.0'

我收到此错误

2019-03-07 10:05:12.020 29278-29278/com.nandrezholdingslimited.pebblar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nandrezholdingslimited.pebblar, PID: 29278
android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class android.support.design.widget.TabLayout
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class android.support.design.widget.TabLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.TabLayout" on path: DexPathList[[zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/base.apk", zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/split_lib_dependencies_apk.apk", zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/split_lib_resources_apk.apk", zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/split_lib_slice_0_apk.apk", zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/split_lib_slice_1_apk.apk", zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/split_lib_slice_2_apk.apk", zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/split_lib_slice_3_apk.apk", zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/split_lib_slice_4_apk.apk", zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/split_lib_slice_5_apk.apk", zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/split_lib_slice_6_apk.apk", zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/split_lib_slice_7_apk.apk", zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/split_lib_slice_8_apk.apk", zip file "/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.nandrezholdingslimited.pebblar-ZCiGV8uuN3ooM7_JoASOUg==/lib/arm64, /system/lib64, /system/vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at com.nandrezholdingslimited.pebblar.features.tripslist.TripsListFragment.onCreateView(TripsListFragment.kt:30)

我从各种stackoverflow帖子中了解到,这可能是由于库版本不兼容问题引起的,但是我尝试了许多不同的版本,并且每当Android尝试将TabLayout填充到Fragment中时,我仍然会崩溃。

请帮助我解决这个问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

好的,我知道问题出在哪里。

如果在创建新项目时选择“使用AndroidX工件”,那么我将不得不使用

implementation 'com.google.android.material:material:1.1.0-alpha04'

com.google.android.material.tabs.TabLayout

如果我使用

implementation 'com.android.support:design:28.0.0'

android.support.design.widget.TabLayout

应用会在使TabLayout膨胀时崩溃。

如果我没有选择“使用AndroidX工件”,那么我可以毫无问题地使用'com.android.support:design:28.0.0'