Inflating class android.support.v4.widget.SwipeRefreshLayout-ThrowExceptions

Exception or error:

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.

How to solve:

This issue seems to happen when migrating to AndroidX as there are many build artifact to be changed when Migrating to AndroidX.

Solution 1: Do it manually

For exemple:

android.support.v4.widget.SwipeRefreshLayout changed to => androidx.swiperefreshlayout.widget.SwipeRefreshLayout

You must update all the build artifact listed in Migrating to AndroidX

Solution 2: Automate on Android Studio 3.2+

As suggested in this same link, you could automate this process by selecting Refactor > Migrate to AndroidX from the menu bar.


ADDITIONAL INFO – The errors I had:

Error inflating class android.support.constraint.ConstraintLayout

element WebView is not allowed here

###

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

###

I was stuck on the SwipeRefreshLayout using AndroidX

I was not able to get Reference of SwipeRefreshLayout in AndroidX

So I added implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
dependency in app level gradle file.

So in my case solution is,

Add the dependency in app level build.gradle file

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'

###

Gradle need to new dependency which is 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;

In you XML file, you should use the tags of SwipeRefreshLayout:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout

@canerkaseler

Leave a Reply

Your email address will not be published. Required fields are marked *