【发布时间】:2019-02-27 12:32:43
【问题描述】:
我为我的 Android 应用创建了一个导航视图,但是当 Activity 尝试启动并崩溃时,我收到以下错误。
我查看了其他用户类似的问题,但我仍然无法确定问题。
任何帮助将不胜感激。
logcat 还指向 task_list.java 的第 60 行,即:
setContentView(R.layout.navigation_drawer);
这最初是指向一个基本的 xml 活动。
Binary XML file line #13: Binary XML file line #13: Error inflating
class android.support.design.widget.NavigationView
Navigation_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include layout="@layout/task_list"/>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/navigation_menu">
</android.support.design.widget.NavigationView>
navigation_header.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="160dp"
android:padding="16dp"
android:gravity="bottom"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:background="@drawable/background">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="56dp"
android:layout_height="56dp"
android:src="@drawable/accountimagecolor"
android:scaleType="centerCrop"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Alex"
android:paddingTop="8dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/emailAlex"
android:padding="4dp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1"/>
</LinearLayout>
v21/styles.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="actionBarPopupTheme">@style/PopupTheme</item>
<item name="android:navigationBarColor">@color/colorPrimary</item>
</style>
<style name="PopupTheme" parent="Theme.AppCompat.Light">
<item name="android:background">@color/background</item>
<item name="android:textColor">@color/black</item>
</style>
</resources>
task_list.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Portstewart - SuperValu Task List");
drawerLayout = findViewById(R.id.drawerLayout);
navigationView = findViewById(R.id.navigation_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer);
drawerLayout.setDrawerListener(toggle);
toggle.syncState();
分级
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.google.firebase:firebase-database:16.0.6'
implementation 'com.google.firebase:firebase-auth:16.1.0'
implementation 'com.firebaseui:firebase-ui-database:4.2.1'
implementation 'com.android.support:mediarouter-v7:28.0.0'
implementation 'de.hdodenhof:circleimageview:3.0.0'
}
apply plugin: 'com.google.gms.google-services'
【问题讨论】:
-
尝试删除此行
app:menu="@menu/navigation_menu"并查看导航是否显示? -
@Swati 我现在可以看到导航栏,没有错误,谢谢,我现在如何显示导航菜单?
-
我认为问题出在
navigation_menu。所以,现在再次添加该行。并在您的item list中逐一添加项目并检查生成item错误 -
@Swati 是的,你是对的,我没有正确构建 navigation_menu(与项目中不包含第二个菜单有关)现在一切正常,应该感谢您的帮助!
标签: java android navigationview