Although there is a similar question, it does not give me a solution. When I'm trying to use com.google.android.material.floatingactionbutton.FloatingActionButton
it runs well. But if I try to use com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
, it gives errors.
My code in the XML file:
<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)
Can you please help me?
From your logcat
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
So add a material theme for your app/current activity will fix this issue
1) add dependency* (latest / more stable version)
implementation 'com.google.android.material:material:1.2.0-alpha01'
2) Create a new theme
<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>
change parent material theme according to you parent="Theme.MaterialComponents.*
3) change app theme or current activity theme in manifest
android:theme="@style/MyMaterialTheme"
or
<activity android:name=".MyActivity" android:theme="@style/MyMaterialTheme"> </activity>
4) If you found any rendering issue in previews, adding themes to ExtendedFloatingActionButton will fix this
Failed to find '@attr/textAppearanceButton' in current theme.
add theme to 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"
/>
For more info visit Material Design Page
You need to use a an app or activity theme
that is a descendant from Theme.MaterialComponents
.
You can see in the trace: Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
Go to your manifest file and make sure your app and activity inherits from one of this themes:
Theme.MaterialComponents
Theme.MaterialComponents.NoActionBar
Theme.MaterialComponents.Light
Theme.MaterialComponents.Light.NoActionBar
Theme.MaterialComponents.Light.DarkActionBar
Theme.MaterialComponents.DayNight
Theme.MaterialComponents.DayNight.NoActionBar
Theme.MaterialComponents.DayNight.DarkActionBar
EG like this:
<activity android:name=".MYActivity"
android:theme="@android:style/Theme.Light.NoTitleBar"></activity>
Here this one work for me
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton android:theme="@style/MyMaterialTheme" .................. />
<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>
Update your app theme to inherit from one of these themes, eg:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
<!-- ... -->
</style>
dependencies {
// ...
implementation "com.google.android.material:material:1.3.0-alpha03"
}
GL
I've got the same error after updating to targetSdkVersion = 32. My activity already had a theme inheriting from MaterialComponents. The issue was that I was setting
android:background="?selectableItemBackground"
to the ExtendedFloatingActionButton. So if you're trying to override the background, it will give you error. Remove that and it should work.
If you plan to make an Application using material Design Componentes, consider using Theme.MaterialComponents.* because in some occasions you will face some inflating erros regarding XML
More information about Material Design Components can be found here: MATERIAL.IO
i had this problem.i checked the style folder and added feature
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.