充气 class android.support.v4.widget.SwipeRefreshLayout

Inflating class android.support.v4.widget.SwipeRefreshLayout

我正在尝试在片段中实现 SwipeRefreshLayout。它看起来创建正确,但我继续收到错误:

Caused by: android.view.InflateException: Binary XML file line #2: 
Binary XML file line #2: Error inflating class 
android.support.v4.widget.SwipeRefreshLayout
Caused by: android.view.InflateException: Binary XML file line #2: 
Error inflating class android.support.v4.widget.SwipeRefreshLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class 
"android.support.v4.widget.SwipeRefreshLayout" on path: 
DexPathList[[zip file "/data/app/com.weather.rainy- 
2/base.apk"],nativeLibraryDirectories=[/data/app/com.weather.rainy- 
2/lib/x86, /system/lib, /vendor/lib]]

我真的不知道是什么原因造成的。

我实现 SwipeRefreshLayout 的片段布局:

    <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipeToRefresh4Maps"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/topRL4Maps"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_gradient_purple"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="64dp"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingBottom="64dp"
        tools:context="com.weather.rainy.ui.MapsActivity">

        <TextView
            android:id="@+id/yourLocationLabelTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15dp"
            android:text="@+string/your_location"
            android:textColor="@android:color/white"
            android:textSize="24sp" />

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/addressValueTextView"
            android:layout_alignBottom="@+id/addressValueTextView"
            android:layout_below="@+id/yourLocationLabelTextView"
            android:layout_centerHorizontal="true"
            android:layout_margin="15dp"
            tools:context="com.weather.rainy.ui.MapsActivity" />

        <TextView
            android:id="@+id/addressValueTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/map"
            android:layout_alignEnd="@+id/map"
            android:layout_alignParentStart="false"
            android:layout_alignParentBottom="true"
            android:text="@+string/..."
            android:textColor="#aaffffff"
            android:textSize="18sp" />
    </RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>

而且,我的片段 class 用于调用 SwipeRefreshLayout:

  //Code for Swipe Refresh
    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeToRefresh4Maps);
    mSwipeRefreshLayout.setColorSchemeResources(R.color.Red, R.color.Orange, R.color.Blue, R.color.Green);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            Log.v(TAG, "************** MAPS - SWIPE REFRESH EVENT TRIGGERED!!!!!");
            findLocation();
        }
    });

我真的不知道怎么了。非常感谢任何帮助。

因为您使用的是 AndroidX,所以您的 XML 引用了错误版本的 SwipeRefreshLayout

将您的 XML 从 android.support.v4.widget.SwipeRefreshLayout 更改为 androidx.swiperefreshlayout.widget.SwipeRefreshLayout

这个问题似乎在迁移到 AndroidX 时发生,因为在迁移到 AndroidX 时有许多构建工件要更改。

解决方案 1:手动执行

例如:

android.support.v4.widget.SwipeRefreshLayout 改为=> androidx.swiperefreshlayout.widget.SwipeRefreshLayout

您必须更新 Migrating to AndroidX

中列出的所有构建工件

解决方案 2:在 Android Studio 3.2+

上自动化

如同一 link 中所建议,您可以通过从菜单栏中选择 Refactor > Migrate to AndroidX 来自动执行此过程。


ADDITIONAL INFO - The errors I had:

Error inflating class android.support.constraint.ConstraintLayout

element WebView is not allowed here

我被困在 SwipeRefreshLayout 使用 AndroidX

我无法在 AndroidX

中获取 SwipeRefreshLayout 的引用

所以我添加了implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0' 应用级别 gradle 文件中的依赖项。

所以在我的情况下,解决方案是,

在应用级别添加依赖build.gradle文件

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'

Gradle 需要新的依赖,即 AndroidX:

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'

If you use SwipeRefreshLayout in Java class, you can see this line: import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

在你的XML文件中,你应该使用SwipeRefreshLayout的标签:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout