web-dev-qa-db-fra.com

Erreur lors du gonflement de la classe com.google.Android.material.floatingactionbutton.ExtendedFloatingActionButton

Bien qu'il y ait une question similaire, elle ne me donne pas de solution. Lorsque j'essaie d'utiliser com.google.Android.material.floatingactionbutton.FloatingActionButton ça marche bien. Mais si j'essaie d'utiliser com.google.Android.material.floatingactionbutton.ExtendedFloatingActionButton, cela donne des erreurs.

Mon code dans le fichier XML:

<com.google.Android.material.floatingactionbutton.ExtendedFloatingActionButton
    Android:id="@+id/book_profile_write_review"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:layout_marginBottom="20dp"
    Android:contentDescription="@string/author_name"
    Android:text="@string/book_name"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:srcCompat="@drawable/ic_launcher_foreground" />

logcat:

2019-11-14 12:30:21.997 21326-21326/com.bookaholic.shahad.bookaholicbd E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bookaholic.shahad.bookaholicbd, PID: 21326
Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bookaholic.shahad.bookaholicbd/com.bookaholic.shahad.bookaholicbd.activities.BookProfile}: Android.view.InflateException: Binary XML file line #25: Binary XML file line #25: Error inflating class com.google.Android.material.floatingactionbutton.ExtendedFloatingActionButton
    at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2974)
    at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:3059)
    at Android.app.ActivityThread.-wrap11(Unknown Source:0)
    at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1724)
    at Android.os.Handler.dispatchMessage(Handler.Java:106)
    at Android.os.Looper.loop(Looper.Java:164)
    at Android.app.ActivityThread.main(ActivityThread.Java:7000)
    at Java.lang.reflect.Method.invoke(Native Method)
    at com.Android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.Java:441)
    at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:1408)
 Caused by: Android.view.InflateException: Binary XML file line #25: Binary XML file line #25: Error inflating class com.google.Android.material.floatingactionbutton.ExtendedFloatingActionButton
 Caused by: Android.view.InflateException: Binary XML file line #25: Error inflating class com.google.Android.material.floatingactionbutton.ExtendedFloatingActionButton
 Caused by: Java.lang.reflect.InvocationTargetException
    at Java.lang.reflect.Constructor.newInstance0(Native Method)
    at Java.lang.reflect.Constructor.newInstance(Constructor.Java:334)
    at Android.view.LayoutInflater.createView(LayoutInflater.Java:647)
    at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:790)
    at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:730)
    at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:863)
    at Android.view.LayoutInflater.rInflateChildren(LayoutInflater.Java:824)
    at Android.view.LayoutInflater.inflate(LayoutInflater.Java:515)
    at Android.view.LayoutInflater.inflate(LayoutInflater.Java:423)
    at Android.view.LayoutInflater.inflate(LayoutInflater.Java:374)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.Java:555)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.Java:161)
    at com.bookaholic.shahad.bookaholicbd.activities.BookProfile.onCreate(BookProfile.Java:20)
    at Android.app.Activity.performCreate(Activity.Java:7258)
    at Android.app.Activity.performCreate(Activity.Java:7249)
    at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1222)
    at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2927)
    at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:3059)
    at Android.app.ActivityThread.-wrap11(Unknown Source:0)
    at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1724)
    at Android.os.Handler.dispatchMessage(Handler.Java:106)
    at Android.os.Looper.loop(Looper.Java:164)
    at Android.app.ActivityThread.main(ActivityThread.Java:7000)
    at Java.lang.reflect.Method.invoke(Native Method)
    at com.Android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.Java:441)
    at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:1408)
 Caused by: Java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
    at com.google.Android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.Java:243)
    at com.google.Android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.Java:217)
    at com.google.Android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.Java:145)
    at com.google.Android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.Java:76)
    at com.google.Android.material.button.MaterialButton.<init>(MaterialButton.Java:200)

Pouvez-vous m'aider s'il vous plaît?

4
Shahad Mahmud

Depuis votre logcat

Caused by: Java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

Donc, ajoutez un thème matériel pour votre application/l'activité actuelle résoudra ce problème

1) ajouter une dépendance * (dernière version/plus stable)

implementation 'com.google.Android.material:material:1.2.0-alpha01'

2) Créez un nouveau thème

<style name="MyMaterialTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Add attributes here -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="textAppearanceButton">@style/AppTextAppearance.Button</item>
</style>
 <!--To fix rendering in preview -->
 <style name="AppTextAppearance.Button" parent="TextAppearance.MaterialComponents.Button">
    <item name="Android:textAllCaps">true</item>
</style>

changer le thème du matériau parent selon vous parent = "Theme.MaterialComponents. *

3) Changer le thème de l'application ou le thème d'activité actuel dans le manifeste

Android:theme="@style/MyMaterialTheme"

ou

<activity Android:name=".MyActivity" Android:theme="@style/MyMaterialTheme"> </activity>

4) Si vous avez trouvé un problème de rendu dans les aperçus, l'ajout de thèmes à ExtendedFloatingActionButton corrigera ce problème.

Failed to find '@attr/textAppearanceButton' in current theme.

ajouter un thème à ExtendedFloatingActionButton Android: theme = "@ style/MyMaterialTheme"

<com.google.Android.material.floatingactionbutton.ExtendedFloatingActionButton
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:theme="@style/MyMaterialTheme"
        Android:text="ExtendedFab"            
        app:icon="@drawable/ic_add_a_photo_white_24dp"
       />

Pour plus d'informations, visitez Page Material Design

3
Adarsh Vijayan P

Vous devez utiliser une application ou une activité theme qui est un descendant de Theme.MaterialComponents.

Vous pouvez voir dans la trace: Caused by: Java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

1
Yoav Gibri