androidswiperefreshlayout

Inflating class android.support.v4.widget.SwipeRefreshLayout


I am trying to implement SwipeRefreshLayout in fragments. It is looking created correctly, but I continue to get the errors:

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]]

I really don't have any idea what is causing this.

My fragment layout for implementing the 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>

And, my fragment class for calling the 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();
        }
    });

I really don't have any clue, what is the wrong. Any help is greatly appreciated.


Solution

  • Because you are using AndroidX, your XML is referencing the wrong version of SwipeRefreshLayout

    Change in your XML from android.support.v4.widget.SwipeRefreshLayout to androidx.swiperefreshlayout.widget.SwipeRefreshLayout