Error inflating class de.hdodenhof.circleimageview.CircleImageView in Android version 6 marshmallow
Asked Answered
G

5

7

I created an app which uses this hdodenhof CircleImageview library. My app works fine on Android 7 and 7.1 but it is crashing in lower versions such as marshmallow/Android 6. How can I solve this problem? I have read his GitHub posts but it did not solve my problem as people has asked to remove attributes from the .xml, but I didn't use these attributes.

Here's my simple implementation of the CircleImageView:

<de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/civ_requests"
        android:layout_width="80sp"
        android:layout_height="80sp"
        android:layout_margin="10sp"
        android:src="@drawable/profilesvg" />

And heres the error:

FATAL EXCEPTION: main
Process: com.milind.locatemymate, PID: 4288
android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class de.hdodenhof.circleimageview.CircleImageView
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.milind.locatemymate.Fragment.Requests$1.onCreateViewHolder(Requests.java:152)
at com.milind.locatemymate.Fragment.Requests$1.onCreateViewHolder(Requests.java:103)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3225)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1632)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)

Same error in friends fragment, heres its updated xml layout:

<?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="wrap_content"
    android:orientation="vertical"
    android:padding="5dp">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <de.hdodenhof.circleimageview.CircleImageView
        android:src="@drawable/profilesvg"
        android:id="@+id/civ_friend"
        android:layout_width="80dp"
        android:layout_height="80dp" />

    <LinearLayout
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:padding="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:textStyle="bold"
            android:textColor="@color/LightBlack"
            android:id="@+id/tv_friendName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:textColor="@color/LightBlack"
            android:layout_marginTop="5dp"
            android:id="@+id/tv_friendStatus"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    </LinearLayout>
    <View
        android:layout_marginTop="5dp"
        android:background="@color/LightGrey"
        android:layout_width="match_parent"
        android:layout_height="0.5dp" />
</LinearLayout>
Glazing answered 29/4, 2018 at 12:15 Comment(1)
This may not be a problem but why are you defined width, height and margin in sp? It supposed to be in dp.Kokoruda
J
7

CHange this:

<de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/civ_requests"
        android:layout_width="80sp"
        android:layout_height="80sp"
        android:layout_margin="10sp"
        android:src="@drawable/profilesvg" />

to this:

<de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/civ_requests"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_margin="10dp"
        android:src="@drawable/profilesvg" />

You're suppose to use SP only in fontSize. For layout height and width as well as margins the unit should be DP.

And with this make sure your source image is in drawable and not in drawablev24.

Jeffcott answered 29/4, 2018 at 12:18 Comment(13)
the error from that requests fragment has gone i suppose, but it shows error in my one more fragment called friends , i just replaced the sp there by dp in cricleimagview but still its showing inflation error over there. How can i show you its xml layout now?Glazing
you can edit your question and add your xml layout thereJeffcott
ok sure , just before proceeding i want to tell you that i have sp for all of my views. I think i had misunderstanding with sp and dp. Should i change sp by dp in all of my views and then try it, apart from textsize in my texviews?Glazing
yep, you should. Remember to only use sp when setting textSize in Views. For all other measures you should use DP :)Jeffcott
yes thanks again, let me change sp by dp in all of my activites and fragments layouts, Then i try to execute again. Then if it doesnt crash ( i hope it wont) , i will accept ur answer mate :)Glazing
hey mate i have changed all views height,width and margins by dp. But still it shows Error inflating class de.hdodenhof.circleimageview.CircleImageView in my friends fragment. i have attached the friends layout in above question. Please have a look at itGlazing
Could you post your complete stack trace?Jeffcott
Hi mate i have solved my problem, it read few couple of stack questions and my issue was i guess putting my image in drawable v24 , then i deleted it and re inserted it this time in drawable rather than drawable v24. But i will still accept your answer , you have tried to solve my problem , and were still doing it , thanks alot :) i wil just add one more line to ur answer :)Glazing
Aw thanks :) I thought the problem might be with multidex, but I saw someone having problems with the drawable 24 folder. Glad you fixed the problem :)Jeffcott
is there anyway i can be in contact with you ?Glazing
i am still running into problems by using dpScrewy
<de.hdodenhof.circleimageview.CircleImageView android:id="@+id/imgVoice" android:layout_width="22dp" android:layout_height="22dp" android:layout_gravity="center" android:src="@drawable/mic_svg" /> me to same problem:Tantalize
The last paragraph eventually saved me @LeviMoreiraCroak
D
9

I also faced the same issue having width and height is "dp". But, on my side it resolves by replacing scaleType from fitcenter to centerCrop.

Duax answered 24/1, 2019 at 9:49 Comment(0)
J
7

CHange this:

<de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/civ_requests"
        android:layout_width="80sp"
        android:layout_height="80sp"
        android:layout_margin="10sp"
        android:src="@drawable/profilesvg" />

to this:

<de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/civ_requests"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_margin="10dp"
        android:src="@drawable/profilesvg" />

You're suppose to use SP only in fontSize. For layout height and width as well as margins the unit should be DP.

And with this make sure your source image is in drawable and not in drawablev24.

Jeffcott answered 29/4, 2018 at 12:18 Comment(13)
the error from that requests fragment has gone i suppose, but it shows error in my one more fragment called friends , i just replaced the sp there by dp in cricleimagview but still its showing inflation error over there. How can i show you its xml layout now?Glazing
you can edit your question and add your xml layout thereJeffcott
ok sure , just before proceeding i want to tell you that i have sp for all of my views. I think i had misunderstanding with sp and dp. Should i change sp by dp in all of my views and then try it, apart from textsize in my texviews?Glazing
yep, you should. Remember to only use sp when setting textSize in Views. For all other measures you should use DP :)Jeffcott
yes thanks again, let me change sp by dp in all of my activites and fragments layouts, Then i try to execute again. Then if it doesnt crash ( i hope it wont) , i will accept ur answer mate :)Glazing
hey mate i have changed all views height,width and margins by dp. But still it shows Error inflating class de.hdodenhof.circleimageview.CircleImageView in my friends fragment. i have attached the friends layout in above question. Please have a look at itGlazing
Could you post your complete stack trace?Jeffcott
Hi mate i have solved my problem, it read few couple of stack questions and my issue was i guess putting my image in drawable v24 , then i deleted it and re inserted it this time in drawable rather than drawable v24. But i will still accept your answer , you have tried to solve my problem , and were still doing it , thanks alot :) i wil just add one more line to ur answer :)Glazing
Aw thanks :) I thought the problem might be with multidex, but I saw someone having problems with the drawable 24 folder. Glad you fixed the problem :)Jeffcott
is there anyway i can be in contact with you ?Glazing
i am still running into problems by using dpScrewy
<de.hdodenhof.circleimageview.CircleImageView android:id="@+id/imgVoice" android:layout_width="22dp" android:layout_height="22dp" android:layout_gravity="center" android:src="@drawable/mic_svg" /> me to same problem:Tantalize
The last paragraph eventually saved me @LeviMoreiraCroak
T
6

In my case I had this error, because I had adjustViewBounds="true" in my layout. Removing it fixed the problem.

Teethe answered 30/4, 2019 at 13:55 Comment(0)
S
3

Okay, on checking the source code and implementation, it seems the developer had intentionally, coded them as I see those might not be supported as of the day this answer was posted here.

    @Override
    public void setScaleType(ScaleType scaleType) {
        if (scaleType != SCALE_TYPE) {
            throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));
        }
    }

    @Override
    public void setAdjustViewBounds(boolean adjustViewBounds) {
        if (adjustViewBounds) {
            throw new IllegalArgumentException("adjustViewBounds not supported.");
        }
    }

and the only supported ScaleType is

private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;

So removing those from the CircularImageView attributes in XML should be a workaround until a fix is implemented.

There is an alternative CircularImageView library by Lopez Mikhael at https://github.com/lopspower/CircularImageView

This library also has some limitation: (at least adjustViewBounds isn't supported yet)

LIMITATIONS

  • By default the ScaleType is FIT_CENTER. You can also use CENTER_INSIDE and CENTER_CROP.

  • Enabling adjustViewBounds is not supported as this requires an unsupported ScaleType.

Sow answered 10/5, 2020 at 1:48 Comment(0)
A
0

I got the same problem and I just add this in build.gradle and the problem is fixed!

dependencies { ... implementation 'de.hdodenhof:circleimageview:3.1.0' }

Attachment answered 7/3, 2023 at 9:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.