簡體   English   中英

充氣錯誤 class com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton

[英]Error inflating class com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton

雖然有類似的問題,但沒有給我解決方案。 當我嘗試使用com.google.android.material.floatingactionbutton.FloatingActionButton時,它運行良好。 但是,如果我嘗試使用com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton ,它會出錯。

我在 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" />

日志:

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)

你能幫我么?

從你的日志貓

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

因此,為您的應用程序/當前活動添加材料主題將解決此問題

1)添加依賴*(最新/更穩定的版本)

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

2)創建一個新主題

<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>

根據您更改父材質主題parent="Theme.MaterialComponents.*

3)在清單中更改應用程序主題或當前活動主題

android:theme="@style/MyMaterialTheme"

或者

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

4) 如果您在預覽中發現任何渲染問題,將主題添加到 ExtendedFloatingActionButton 將解決此問題

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

將主題添加到 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"
       />

欲了解更多信息,請訪問材料設計頁面

您需要使用來自Theme.MaterialComponents的應用程序或活動theme

您可以在跟蹤中看到: Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).

Go 到您的清單文件,並確保您的應用程序和活動繼承自以下主題之一:

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:

<activity android:name=".MYActivity"
        android:theme="@android:style/Theme.Light.NoTitleBar"></activity>

這對我有用

 <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>

AndroidManifest.xml

更新您的應用主題以繼承這些主題之一,例如:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <!-- ... -->
</style>

build.gradle

dependencies {
    // ...
    implementation "com.google.android.material:material:1.3.0-alpha03"
}

總帳

資源

更新到 targetSdkVersion = 32 后,我遇到了同樣的錯誤。我的活動已經有一個從 MaterialComponents 繼承的主題。 問題是我正在設置

android:background="?selectableItemBackground"

到 ExtendedFloatingActionButton。 所以如果你試圖覆蓋背景,它會給你錯誤。 刪除它,它應該可以工作。

如果您打算使用材料設計組件制作應用程序,請考慮使用 Theme.MaterialComponents.*,因為在某些情況下,您將面臨一些關於 XML 的膨脹錯誤

有關材料設計組件的更多信息,請參見: MATERIAL.IO

我遇到了這個問題。我檢查了樣式文件夾並添加了功能

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM