当先锋百科网

首页 1 2 3 4 5 6 7

项目场景:

Caused by: android.view.InflateException: Binary XML file line #61 in com.example.musicplayer:layout/activity_play: Binary XML file line #61 in com.example.musicplayer:layout/activity_play: Error inflating class android.support.v4.view.ViewPager
Caused by: android.view.InflateException: Binary XML file line #61 in com.example.musicplayer:layout/activity_play: Error inflating class android.support.v4.view.ViewPager
Caused by: java.lang.ClassNotFoundException: android.support.v4.view.ViewPager


问题描述:

问题代码:

 <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/MusicTime"
        android:layout_below="@id/tool"/>

错误日志:

     Caused by: android.view.InflateException: Binary XML file line #61 in com.example.musicplayer:layout/activity_play: Binary XML file line #61 in com.example.musicplayer:layout/activity_play: Error inflating class android.support.v4.view.ViewPager
     Caused by: android.view.InflateException: Binary XML file line #61 in com.example.musicplayer:layout/activity_play: Error inflating class android.support.v4.view.ViewPager
     Caused by: java.lang.ClassNotFoundException: android.support.v4.view.ViewPager
 

原因分析:

Error inflating class android.support.v4.view.ViewPager表示该类产生错误,具体原因可能为,在项目中没有导入support-v4版本的包。或者AndroidStdio版本过高,不支持旧版本包


解决方案:

一、导包:

  1. 点击该图标

在这里插入图片描述2.选择Dependencies, 然后点击右边加号选择第一个Library Dependency

在这里插入图片描述
3.搜索support-v4版本的包并导入。在这里插入图片描述

二、换控件名称:

新版本Android 支持 androidx.viewpager.widget.ViewPager
将android.support.v4.view.ViewPager改为androidx.viewpager.widget.ViewPager即可。

 <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/MusicTime"
        android:layout_below="@id/tool"/>