Material-components-android: [TextView] Crash when using custom style

Created on 6 Aug 2020  路  7Comments  路  Source: material-components/material-components-android

Description:
I am facing a crash at TextView after update my Material Android Library to version 1.2.0 from 1.1.0.
Error Code:

Error inflating class TextView
Caused by: java.lang.IllegalArgumentException: Font {path=null, style=FontStyle { weight=400, slant=0}, ttcIndex=0, axes=, localeList=, buffer=java.nio.DirectByteBuffer[pos=0 lim=433584 cap=433584]} has already been added

Source code:
This is my TextView element.

<TextView
    android:id="@+id/dashboardMessageTV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFF3EF"
    android:drawableStart="@drawable/ic_info"
    android:drawablePadding="4dp"
    android:gravity="start|center_vertical"
    android:paddingStart="4dp"
    android:paddingTop="8dp"
    android:paddingEnd="4dp"
    android:paddingBottom="8dp"
    android:textAlignment="gravity"
    android:textAppearance="@style/CustomTextParagraph"
    android:visibility="gone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

And this is my CustomTextParagraph style.

<style name="CustomTextParagraph" parent="@android:style/TextAppearance.Small">
    <item name="android:textStyle">normal</item>
    <item name="android:fontFamily">@font/sf_pro</item>
    <item name="android:textColor">@color/darkGray</item>
</style>

This is my theme parent.

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">

Android API version:
Android 10 (API level 29)

Material Library version:
implementation 'com.google.android.material:material:1.2.0'

Device:
Xiao Mi 9

bug

Most helpful comment

The issue should be here because both font elements have the same values for the fontStyle and fontWeight:

    <font
        app:font="@font/sf_pro_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />
    <font
        app:font="@font/sf_pro_medium"
        app:fontStyle="normal"
        app:fontWeight="400" />

All 7 comments

Check your @font/sf_pro.

This kind of error in shown when you try to register multiple fonts with the same style and weight into the same font family (and it is related to androidx.core version 1.2.0 and higher).

I've tested on master with your XMLs and the Roboto-Thin.ttf and is working fine.
What suggested by @gabrielemariotti is probably the cause of your crash

Check your @font/sf_pro.

This kind of error in shown when you try to register multiple fonts with the same style and weight into the same font family (and it is related to androidx.core version 1.2.0 and higher).

I agree, so did I need to seperate them to different style or others solution that can keep using same style? Because my whole project is using the same style.

Here is my @font/sf_pro.

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
    <font
        app:font="@font/sf_pro_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />
    <font
        app:font="@font/sf_pro_regular"
        app:fontStyle="italic"
        app:fontWeight="400" />
    <font
        app:font="@font/sf_pro_medium"
        app:fontStyle="normal"
        app:fontWeight="400" />
    <font
        app:font="@font/sf_pro_bold"
        app:fontStyle="normal"
        app:fontWeight="700" />
</font-family>

The issue should be here because both font elements have the same values for the fontStyle and fontWeight:

    <font
        app:font="@font/sf_pro_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />
    <font
        app:font="@font/sf_pro_medium"
        app:fontStyle="normal"
        app:fontWeight="400" />

I think that the issue can be closed since it is not related to the library.

Thanks @gabrielemariotti

The issue should be here because both font elements have the same values for the fontStyle and fontWeight:

    <font
        app:font="@font/sf_pro_regular"
        app:fontStyle="normal"
        app:fontWeight="400" />
    <font
        app:font="@font/sf_pro_medium"
        app:fontStyle="normal"
        app:fontWeight="400" />

Thank you so much for your guidance馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabrielemariotti picture gabrielemariotti  路  3Comments

sepehr-alipour picture sepehr-alipour  路  3Comments

magnusfernandes picture magnusfernandes  路  3Comments

jaychang0917 picture jaychang0917  路  3Comments

TdevM picture TdevM  路  3Comments