我正在尝试使用此库将BottomNavigationView
添加到布局中:
implementation 'com.google.android.material:material:1.0.0'
这是我的布局:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".home.HomeActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:itemBackground="?colorPrimary"
app:itemIconTint="@drawable/bottom_navigation_selector"
app:itemTextColor="@drawable/bottom_navigation_selector"
app:layout_insetEdge="bottom"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
app:menu="@menu/bottom_navigation" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_model"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:fabSize="normal"
app:layout_anchor="@id/bottom_navigation"
app:layout_anchorGravity="end"
app:srcCompat="@drawable/ic_add_white_24dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
它在我的模拟器上运行,但是当我想在真实设备上启动应用程序时,会出现此错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rightechco.rayanpairpanel/com.rightechco.rayanpairpanel.home.HomeActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2489)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2551)
at android.app.ActivityThread.access$1000(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5572)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:968)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763)
Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
at android.view.LayoutInflater.createView(LayoutInflater.java:637)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.rightechco.rayanpairpanel.home.HomeActivity.onCreate(HomeActivity.kt:23)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2551)
at android.app.ActivityThread.access$1000(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5572)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:968)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:611)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.rightechco.rayanpairpanel.home.HomeActivity.onCreate(HomeActivity.kt:23)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2551)
at android.app.ActivityThread.access$1000(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5572)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:968)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f07007b
at android.content.res.Resources.getValue(Resources.java:1446)
at android.content.res.MiuiResources.getValue(MiuiResources.java:146)
at androidx.appcompat.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:331)
at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198)
at androidx.appcompat.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:191)
at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:102)
at androidx.appcompat.view.menu.MenuItemImpl.getIcon(MenuItemImpl.java:505)
at com.google.android.material.bottomnavigation.BottomNavigationItemView.initialize(BottomNavigationItemView.java:105)
at com.google.android.material.bottomnavigation.BottomNavigationMenuView.buildMenuView(BottomNavigationMenuView.java:523)
at com.google.android.material.bottomnavigation.BottomNavigationPresenter.updateMenuView(BottomNavigationPresenter.java:62)
at com.google.android.material.bottomnavigation.BottomNavigationView.inflateMenu(BottomNavigationView.java:254)
at com.google.android.material.bottomnavigation.BottomNavigationView.<init>(BottomNavigationView.java:187)
at com.google.android.material.bottomnavigation.BottomNavigationView.<init>(BottomNavigationView.java:114)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:611)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.rightechco.rayanpairpanel.home.HomeActivity.onCreate(HomeActivity.kt:23)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2551)
at android.app.ActivityThread.access$1000(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1432)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5572)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:968)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:763)
编辑: 这是我的活动代码,如果有帮助的话:
class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
setSupportActionBar(toolbar)
handleBottomNavigationView()
}
private fun handleBottomNavigationView() {
bottom_navigation.setOnNavigationItemSelectedListener {
val id = it.itemId
when (id) {
R.id.bottom_navigation_orders -> openOrderFragment()
R.id.bottom_navigation_tariffs -> openTariffFragment()
R.id.bottom_navigation_laptops -> openLaptopFragment()
R.id.bottom_navigation_mobiles -> openMobileFragment()
else -> false
}
}
}
Edit2: 这就是我所有的依靠:
dependencies {
// Project
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// Support Libraries
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
// Animations
implementation 'com.github.florent37:viewanimator:1.0.5'
// Lifecycle Aware Components
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
kapt 'androidx.lifecycle:lifecycle-compiler:2.0.0'
// User Interface
implementation 'com.github.yalantis:ucrop:2.2.2'
implementation 'net.steamcrafted:load-toast:1.0.12'
implementation 'com.afollestad.material-dialogs:core:2.0.0-beta4'
implementation 'com.github.alxrm:audiowave-progressbar:0.9.2'
implementation 'com.aurelhubert:ahbottomnavigation:2.1.0'
// Dependency Injection
implementation 'com.google.dagger:dagger:2.16'
kapt 'com.google.dagger:dagger-compiler:2.16'
// Runtime Permissions
implementation 'com.github.hotchemi:permissionsdispatcher:4.0.0-alpha1'
kapt 'com.github.hotchemi:permissionsdispatcher-processor:4.0.0-alpha1'
// Rest Services
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
// Image Processing
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'jp.wasabeef:picasso-transformations:2.2.1'
// Debugging
implementation 'com.jakewharton.timber:timber:4.7.1'
// Testing
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-beta02'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-beta02'
}
答案 0 :(得分:4)
确保将可绘制对象放在res/drawable
文件夹中,而不要放在res/drawable-v24
中。我遇到了类似的问题,并且通过将文件移动到res/drawable
文件夹中,错误消失了。
答案 1 :(得分:3)
代替
app:itemBackground="?colorPrimary"
设置
app:itemBackground="@color/colorPrimary"
编辑:还要检查可绘制对象是否位于res/drawable
文件夹中,而不是位于res/drawable-v21
之类的文件夹中
答案 2 :(得分:1)
您的问题与您在 BottomNavigationView 小部件上使用的资源有关。
问题之一是问题:
app:itemBackground="?colorPrimary"
app:itemIconTint="@drawable/bottom_navigation_selector"
app:itemTextColor="@drawable/bottom_navigation_selector"
检查可绘制对象是否同时存在于 res / drawable 和可能存在的任何 res / drawable-vXX 中。