android 中的错误:膨胀 class android.support.design.widget.NavigationView 时出错

error in android: Error inflating class android.support.design.widget.NavigationView

我遇到了这个问题,我尝试了很多解决方案,但它们都不起作用。
xml

<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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_container"/>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_menu" />

</android.support.v4.widget.DrawerLayout>

build.gradle

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.jakewharton:butterknife:8.8.1'
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'
}

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.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="alertDialogTheme">@style/AppTheme.Dialog.Alert</item>
</style>

<style name="AppTheme.Dialog.Alert" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textColorPrimary">@color/colorPrimary</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.TextView" />

<style name="AppTheme.TextView.1" parent="AppTheme.TextView">
    <item name="android:textSize">15sp</item>
</style>

<style name="AppTheme.TextView.1.White" parent="AppTheme.TextView.1">
    <item name="android:textColor">@android:color/white</item>
</style>

<style name="AppTheme.TextView.2" parent="AppTheme.TextView">
    <item name="android:textSize">17sp</item>
</style>

<style name="AppTheme.TextView.2.White" parent="AppTheme.TextView.2">
    <item name="android:textColor">@android:color/white</item>
</style>

<style name="AppTheme.TextView.2.Dark" parent="AppTheme.TextView.2">
    <item name="android:textColor">@color/text_dark</item>
</style>

<style name="AppTheme.TextView.3" parent="AppTheme.TextView">
    <item name="android:textSize">19sp</item>
</style>

<style name="AppTheme.TextView.3.White" parent="AppTheme.TextView.3">
    <item name="android:textColor">@android:color/white</item>
</style>

<style name="AppTheme.TextView.Clickable">
    <item name="android:background">?attr/selectableItemBackground</item>
    <item name="android:padding">@dimen/spacing_small</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:textStyle">bold</item>
</style>

<style name="AppTheme.TextView.Clickable.1" parent="AppTheme.TextView.Clickable">
    <item name="android:textSize">15sp</item>
</style>
<style name="AppTheme.TextView.Clickable.1.White" parent="AppTheme.TextView.Clickable.1">
    <item name="android:textColor">@android:color/white</item>
</style>

<style name="AppTheme.TextView.Clickable.2" parent="AppTheme.TextView.Clickable">
    <item name="android:textSize">17sp</item>
</style>
<style name="AppTheme.TextView.Clickable.2.White" parent="AppTheme.TextView.Clickable.2">
    <item name="android:textColor">@android:color/white</item>
</style>

MainActivity.java

public class MainActivity extends AppCompatActivity {

@BindView(R.id.toolbar) Toolbar toolbar;
@BindView(R.id.drawer_layout) DrawerLayout drawerLayout;
@BindView(R.id.drawer) NavigationView navigationView;

private ActionBarDrawerToggle drawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    setupDrawer();

    if (savedInstanceState == null) {
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_container, ShotListFragment.newInstance())
                .commit();
    }
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void setupDrawer() {
    drawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            drawerLayout,          /* DrawerLayout object */
            R.string.open_drawer,         /* "open drawer" description */
            R.string.close_drawer         /* "close drawer" description */
    );
    drawerLayout.addDrawerListener(drawerToggle);

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            if (item.isChecked()) {
                drawerLayout.closeDrawers();
                return true;
            }

            Fragment fragment = null;
            switch (item.getItemId()) {
                case R.id.drawer_item_home:
                    fragment = ShotListFragment.newInstance();
                    setTitle(R.string.title_home);
                    break;
                case R.id.drawer_item_likes:
                    fragment = ShotListFragment.newInstance();
                    setTitle(R.string.title_likes);
                    break;
                case R.id.drawer_item_buckets:
                    fragment = BucketListFragment.newInstance();
                    setTitle(R.string.title_buckets);
                    break;
            }

            drawerLayout.closeDrawers();

            if (fragment != null) {
                getSupportFragmentManager()
                        .beginTransaction()
                        .replace(R.id.fragment_container, fragment)
                        .commit();
                return true;
            }

            return false;
        }
    });
}
}

抛出了如下三个异常: 原因:

android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class android.support.design.widget.NavigationView

如有任何帮助,我们将不胜感激!

试试这个代码..

<?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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main2"
    app:menu="@menu/activity_main2_drawer" />

</android.support.v4.widget.DrawerLayout>

appbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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=".Main2Activity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main2" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

 </android.support.design.widget.CoordinatorLayout>

nav_hearder.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/nav_header_desc"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    app:srcCompat="@mipmap/ic_launcher_round" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:text="@string/nav_header_title"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/nav_header_subtitle" />

</LinearLayout>

最后这个 content.xml 布局将这个 id 传递给所有片段..

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".Main2Activity"
tools:showIn="@layout/app_bar_main2">

</android.support.constraint.ConstraintLayout>

添加以下依赖 -

annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'