将 BadgeDrawable 与 BottomNavigationView 一起使用时出现 UnsupportedOperationException
UnsupportedOperationException while using BadgeDrawable with BottomNavigationView
我正在使用 BadgeDrawable
和 BottomNavigationView
。但是当我在 bottomNavigationView 的图标中放置一个徽章时,我得到了这个异常
java.lang.UnsupportedOperationException: Failed to resolve attribute at index 3: TypedValue{t=0x2/d=0x7f0400db a=6}
at android.content.res.TypedArray.getColorStateList(TypedArray.java:597)
at com.google.android.material.resources.MaterialResources.getColorStateList(MaterialResources.java:76)
at com.google.android.material.resources.TextAppearance.<init>(TextAppearance.java:85)
at com.google.android.material.badge.BadgeDrawable$SavedState.<init>(BadgeDrawable.java:222)
at com.google.android.material.badge.BadgeDrawable.<init>(BadgeDrawable.java:424)
at com.google.android.material.badge.BadgeDrawable.createFromAttributes(BadgeDrawable.java:332)
at com.google.android.material.badge.BadgeDrawable.create(BadgeDrawable.java:300)
at com.google.android.material.bottomnavigation.BottomNavigationMenuView.getOrCreateBadge(BottomNavigationMenuView.java:692)
at com.google.android.material.bottomnavigation.BottomNavigationView.getOrCreateBadge(BottomNavigationView.java:689)
at com.app.UI.MainActivity.onNavigationItemSelected(MainActivity.java:129)
at com.google.android.material.bottomnavigation.BottomNavigationView.onMenuItemSelected(BottomNavigationView.java:241)
at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
at com.google.android.material.bottomnavigation.BottomNavigationMenuView.onClick(BottomNavigationMenuView.java:131)
at android.view.View.performClick(View.java:8160)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access00(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
当请求成功时,我在 LiveData
的 Observe
回调中设置徽章数量。
这是我的观点
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/bottom_navigation"
android:background="@android:color/transparent"
app:menu="@menu/bottom_navigation_menu"
app:itemBackground="@drawable/navigation_background_selector"
app:itemIconTint="@color/text_color_drawer_selector"
android:padding="5dp"
app:labelVisibilityMode="unlabeled"
app:elevation="0dp"
/>
我如何设置徽章的数量
int menuItemId = bottom_navigation.getMenu().getItem(1).getItemId();
BadgeDrawable badge = bottom_navigation.getOrCreateBadge(menuItemId);
badge.setNumber(20);
还有我的风格
<style name="Theme.app" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="colorAccent">@color/colorAccent</item>
</style>
我通过更改主题解决了这个异常
发件人:
Theme.AppCompat.Light.NoActionBar
收件人:
Theme.MaterialComponents.Light.NoActionBar
我正在使用 BadgeDrawable
和 BottomNavigationView
。但是当我在 bottomNavigationView 的图标中放置一个徽章时,我得到了这个异常
java.lang.UnsupportedOperationException: Failed to resolve attribute at index 3: TypedValue{t=0x2/d=0x7f0400db a=6}
at android.content.res.TypedArray.getColorStateList(TypedArray.java:597)
at com.google.android.material.resources.MaterialResources.getColorStateList(MaterialResources.java:76)
at com.google.android.material.resources.TextAppearance.<init>(TextAppearance.java:85)
at com.google.android.material.badge.BadgeDrawable$SavedState.<init>(BadgeDrawable.java:222)
at com.google.android.material.badge.BadgeDrawable.<init>(BadgeDrawable.java:424)
at com.google.android.material.badge.BadgeDrawable.createFromAttributes(BadgeDrawable.java:332)
at com.google.android.material.badge.BadgeDrawable.create(BadgeDrawable.java:300)
at com.google.android.material.bottomnavigation.BottomNavigationMenuView.getOrCreateBadge(BottomNavigationMenuView.java:692)
at com.google.android.material.bottomnavigation.BottomNavigationView.getOrCreateBadge(BottomNavigationView.java:689)
at com.app.UI.MainActivity.onNavigationItemSelected(MainActivity.java:129)
at com.google.android.material.bottomnavigation.BottomNavigationView.onMenuItemSelected(BottomNavigationView.java:241)
at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
at com.google.android.material.bottomnavigation.BottomNavigationMenuView.onClick(BottomNavigationMenuView.java:131)
at android.view.View.performClick(View.java:8160)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access00(View.java:888)
at android.view.View$PerformClick.run(View.java:30236)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
当请求成功时,我在 LiveData
的 Observe
回调中设置徽章数量。
这是我的观点
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/bottom_navigation"
android:background="@android:color/transparent"
app:menu="@menu/bottom_navigation_menu"
app:itemBackground="@drawable/navigation_background_selector"
app:itemIconTint="@color/text_color_drawer_selector"
android:padding="5dp"
app:labelVisibilityMode="unlabeled"
app:elevation="0dp"
/>
我如何设置徽章的数量
int menuItemId = bottom_navigation.getMenu().getItem(1).getItemId();
BadgeDrawable badge = bottom_navigation.getOrCreateBadge(menuItemId);
badge.setNumber(20);
还有我的风格
<style name="Theme.app" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="colorAccent">@color/colorAccent</item>
</style>
我通过更改主题解决了这个异常
发件人:
Theme.AppCompat.Light.NoActionBar
收件人:
Theme.MaterialComponents.Light.NoActionBar