我正在将ViewPager添加到我在Android Studio中的应用中,但是当我尝试运行它时,它会抛出ClassNotFoundException重新分类为“ 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人工制品:
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"
}
真的很欢迎任何帮助:)
答案 0 :(得分:2)
如果您使用的是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"/>
答案 1 :(得分:1)
对于androidX,viewpager的支持库已替换为
androidx.viewpager:viewpager:1.0.0
用gradle替换您的依赖项。
您正在混合androidX库和支持库,请使用其中之一。用androidX one更新非androidX依赖关系或用支持库替换androidX。