简体   繁体   中英

Error inflating class com.google.android.material.internal.NavigationMenuItemView

this is my first question. If I forget to post anything that might be helpful, please let me know. I'm currently working on a project converting an older UI to one with a navigation drawer. However, I keep getting this error. I've spent a while now trying to figure this out. Thank you anyone in advance for any amount of time you spend on this!

The Exception:

android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class com.google.android.material.internal.NavigationMenuItemView
        at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at com.google.android.material.internal.NavigationMenuPresenter$NormalViewHolder.<init>(NavigationMenuPresenter.java:315)
        at com.google.android.material.internal.NavigationMenuPresenter$NavigationMenuAdapter.onCreateViewHolder(NavigationMenuPresenter.java:411)
        at com.google.android.material.internal.NavigationMenuPresenter$NavigationMenuAdapter.onCreateViewHolder(NavigationMenuPresenter.java:361)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4194)
        at android.view.View.layout(View.java:16653)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at android.view.View.layout(View.java:16653)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1255)
        at android.view.View.layout(View.java:16653)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at android.view.View.layout(View.java:16653)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
        at android.view.View.layout(View.java:16653)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at android.view.View.layout(View.java:16653)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
        at android.view.View.layout(View.java:16653)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
        at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
        at android.view.View.layout(View.java:16653)
        at android.view.ViewGroup.layout(ViewGroup.java:5437)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
        at android.view.Choreographer$CallbackRecord.ru

activity_main.xml

<androidx.drawerlayout.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">

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


    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/activity_main_drawer"
        app:headerLayout="@layout/nav_header_main" />
</androidx.drawerlayout.widget.DrawerLayout>

app_bar_main.xml

<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=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/color_red"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

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

</androidx.coordinatorlayout.widget.CoordinatorLayout>

content_main.xml

<androidx.constraintlayout.widget.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_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <include layout="@layout/home_main_menu"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main_drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/Track"
            android:title="@string/text_track" />
        <item
            android:id="@+id/Landmark"
            android:title="@string/text_landmark" />
        <item
            android:id="@+id/ZoneTimer"
            android:title="@string/text_zone_timer" />
        <item
            android:id="@+id/StatsMenu"
            android:title="@string/text_stats" />
        <item
            android:id="@+id/SettingsFragment"
            android:title="@string/text_settings" />
    </group>
</menu>

MainActivity.java

@Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate()");
        // Do super class actions
        super.onCreate(savedInstanceState);

        // Set up the basic Android full screen properties
        initialScreenSetup();

        // Launch the Home Screen
        setContentView(R.layout.activity_main);

        // Set up Navigation Controls
        setupNavigationControl();
}


private void setupNavigationControl() {

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Set up the Navigation Components
        mNavController = Navigation.findNavController(this, R.id.nav_host_fragment);
        // Add the Destination Change Listener to monitor changes
        mNavController.addOnDestinationChangedListener(this::onDestinationChanged);


        DrawerLayout drawer = findViewById(R.id.drawer_layout);

           mAppBarConfiguration = new AppBarConfiguration.Builder(mNavController.getGraph())
                .setDrawerLayout(drawer)
                .build();

        mNavigationView = findViewById(R.id.nav_view);
        NavigationUI.setupActionBarWithNavController(this, mNavController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(mNavigationView, mNavController);
    }

尝试将整个项目迁移到 Androidx:转到 android studio 菜单,然后选择“重构”并单击“迁移到 androidx”项 选择备份并单击重构 重构并运行您的项目

So I had a senior engineer friend look at it. Of course he figured it out in like 10 minutes. My problem was the theme. I guess the theme I was using for the drawer was referencing one that conflicts with the rest of the project. Normally, the specified

@style/AppTheme.AppBarOverlay

and

@style/AppTheme.PopupOverlay

would be fine, but this specific project was using an older, conflicting material. Thanks again for anyone who took time to look at my issue. I love stack overflow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM