【问题标题】:ClassNotFoundException re android.support.v4.view.ViewPager when inflatingClassNotFoundException re android.support.v4.view.ViewPager 充气时
【发布时间】:2019-07-04 20:36:18
【问题描述】:

我在 Android Studio 中将 ViewPager 添加到我的应用,但是当我尝试运行它时,它会抛出 ClassNotFoundException re class "android.support.v4.view.ViewPager"。

已经做了很多谷歌搜索并尝试更改 build.gradle 中的依赖项,但没有任何改变我看到的错误。

以下是来自 Logcat 的相关错误行:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.playmusic/com.example.android.playmusic.MainActivity}: android.view.InflateException: Binary XML file line #60: Binary XML file line #60: Error inflating class android.support.v4.view.ViewPager
...
Caused by: android.view.InflateException: Binary XML file line #60: Binary XML file line #60: Error inflating class android.support.v4.view.ViewPager
Caused by: android.view.InflateException: Binary XML file line #60: Error inflating class android.support.v4.view.ViewPager
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.view.ViewPager" on path: DexPathList[[zip file "/data/app/com.example.android.playmusic--Sh7PlYKQR0g2meRQdvXcg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.android.playmusic--Sh7PlYKQR0g2meRQdvXcg==/lib/arm64, /system/lib64, /system/vendor/lib64]]

这是 main_activity.xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/main_tabs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:tabMode="scrollable">

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab0" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab1" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tab2" />

    </com.google.android.material.tabs.TabLayout>

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

</LinearLayout>

这是我在 build.gradle 中的依赖项。如您所见,我尝试使用 androidx 和 com.android.support 版本的 viewpager artefact:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation "com.android.support:design:28.0.0"
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'com.loopj.android:android-async-http:1.4.9'
    implementation "android.arch.lifecycle:extensions:1.1.0"
    implementation "android.arch.lifecycle:viewmodel:1.1.0"
    //implementation "androidx.viewpager:viewpager:1.0.0"
    implementation "com.android.support:viewpager:28.0.0"
}

非常欢迎任何帮助:)

【问题讨论】:

    标签: java android android-studio gradle


    【解决方案1】:

    如果您使用的是implementation "androidx.viewpager:viewpager:1.0.0"

    另外,XML 文件也发生了变化

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"/>
    

    【讨论】:

    • 已经改变了 gradle 和布局,现在它可以工作了,感谢@Kishore 和 Karan :)
    • 你不必再在 gradle 文件中包含依赖项了
    【解决方案2】:

    对于 viewpager 的 androidX 支持库已替换为

    androidx.viewpager:viewpager:1.0.0

    在你的 gradle 中用这个替换你的依赖。

    您正在混合 androidX 和支持库的库,使用其中任何一个。使用 androidX 更新非 androidX 依赖项或将 androidX 替换为支持库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多