Java ClassNotFoundException re android.support.v4.view.ViewPager 充气时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54629053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:06:08  来源:igfitidea点击:

ClassNotFoundException re android.support.v4.view.ViewPager when inflating

javaandroidandroid-studiogradle

提问by Andy

I am adding a ViewPager to my app in Android Studio but when I try to run it, it's throwing a ClassNotFoundException re class "android.support.v4.view.ViewPager".

我在 Android Studio 中向我的应用程序添加了一个 ViewPager,但是当我尝试运行它时,它抛出一个 ClassNotFoundException 类“android.support.v4.view.ViewPager”。

Have done lots of googling and tried changing the dependencies in build.gradle, but nothing has changed the error I'm seeing.

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

Here are the relevant error lines from Logcat:

以下是来自 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]]

Here is the main_activity.xml layout:

这是 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>

Here are my dependencies in build.gradle. As you can see I have tried using both androidx and com.android.support versions of viewpager artefact:

这是我在 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"
}

Any help would be really welcome :)

任何帮助都会非常受欢迎:)

采纳答案by Kishore Jethava

If you are using implementation "androidx.viewpager:viewpager:1.0.0"

如果您正在使用 implementation "androidx.viewpager:viewpager:1.0.0"

Also, change in XML file too

此外,更改 XML 文件

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

回答by karan

For androidX support library for viewpager has been replaced with

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

androidx.viewpager:viewpager:1.0.0

androidx.viewpager:viewpager:1.0.0

Replace your dependency with this in your gradle.

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

You are mixing libraries for androidX and support library, Use either one of them. Update non-androidX dependency with androidX one or replace androidX with support libraries.

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