Error inflating menu in navigation Drawer in Nougat and Oreo version

Asked

Viewed 315 times

1

Hello, I have a problem with Android Navigationdrawer in versions 7 or higher. When I add the menu via xml, an Exception is generated. However, in the MARSHMALLOW version it works correctly.

Follows my code:

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

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


    <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"
        android:visibility="visible"
        app:headerLayout="@layout/nav_header_livros"
        app:menu="@menu/activity_livros_drawer"/>
</android.support.v4.widget.DrawerLayout>

content_livros.xml

<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"
    tools:layout_editor_absoluteY="25dp">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="384dp"
        android:layout_height="?attr/actionBarSize"
        android:elevation="0dp"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <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>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="384dp"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/app_bar"
        app:tabSelectedTextColor="#FFF"
        app:tabTextColor="#FFF" />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_below="@id/tab_layout"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tab_layout"
        app:layout_constraintVertical_bias="0.518" />

    <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_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.constraint.ConstraintLayout>

menu/activity_libros_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<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/nav_camera"
            android:icon="@drawable/ic_menu_camera"
            android:title="Import" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Slideshow" />
        <item
            android:id="@+id/nav_manage"
            android:icon="@drawable/ic_menu_manage"
            android:title="Tools" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>

Livrosactivity.java

package br.com.whereit.orvil.Activities;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import br.com.whereit.orvil.Adapters.LivrosTabAdapter;
import br.com.whereit.orvil.R;

public class LivrosActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, TabLayout.OnTabSelectedListener {

    Toolbar toolbar;
    TabLayout tabLayout;
    ViewPager viewPager;
    LivrosTabAdapter livrosTabAdapter;
    DrawerLayout drawer;
    NavigationView navigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_livros);
        toolbar = findViewById(R.id.toolbar);
        tabLayout = findViewById(R.id.tab_layout);
        viewPager = findViewById(R.id.pager);

        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Livros");

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
//
        tabLayout.addTab(tabLayout.newTab().setText("Lendo"));
        tabLayout.addTab(tabLayout.newTab().setText("Quero Ler"));
        tabLayout.addTab(tabLayout.newTab().setText("Lidos"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        livrosTabAdapter = new LivrosTabAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setOffscreenPageLimit(3);
        viewPager.setAdapter(livrosTabAdapter);

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        if(drawer==null) {
            drawer = findViewById(R.id.drawer_layout);
        }
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.livros, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();


        if(drawer==null){
            drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Exception generated when opening Activity

01-26 09:19:02.151 25900-25900/br.com.whereit.orvil E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: br.com.whereit.orvil, PID: 25900
                                                                      android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.internal.NavigationMenuItemView
                                                                      Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.design.internal.NavigationMenuItemView
                                                                      Caused by: java.lang.reflect.InvocationTargetException
                                                                          at java.lang.reflect.Constructor.newInstance0(Native Method)
                                                                          at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
                                                                          at android.view.LayoutInflater.createView(LayoutInflater.java:645)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                          at android.support.design.internal.NavigationMenuPresenter$NormalViewHolder.<init>(NavigationMenuPresenter.java:302)
                                                                          at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.onCreateViewHolder(NavigationMenuPresenter.java:402)
                                                                          at android.support.design.internal.NavigationMenuPresenter$NavigationMenuAdapter.onCreateViewHolder(NavigationMenuPresenter.java:352)
                                                                          at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6493)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
                                                                          at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
                                                                          at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
                                                                          at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
                                                                          at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
                                                                          at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
                                                                          at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
                                                                          at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)
                                                                          at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)
                                                                          at android.view.View.layout(View.java:17642)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                          at android.view.View.layout(View.java:17642)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1196)
                                                                          at android.view.View.layout(View.java:17642)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                          at android.view.View.layout(View.java:17642)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                          at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                          at android.view.View.layout(View.java:17642)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                                                                          at android.view.View.layout(View.java:17642)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
                                                                          at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
                                                                          at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
                                                                          at android.view.View.layout(View.java:17642)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
                                                                          at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
01-26 09:19:02.159 25900-25900/br.com.whereit.orvil E/AndroidRuntime:     at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
                                                                          at android.view.View.layout(View.java:17642)
                                                                          at android.view.ViewGroup.layout(ViewGroup.java:5575)
                                                                          at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2361)
                                                                          at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2083)
                                                                          at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1269)
                                                                          at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6382)
                                                                          at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                                                                          at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                                                                          at android.view.Choreographer.doFrame(Choreographer.java:621)
                                                                          at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                                                                          at android.os.Handler.handleCallback(Handler.java:751)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:159)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6146)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                       Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 15 to dimension: type=0x1
                                                                          at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:720)
                                                                          at android.view.View.<init>(View.java:4213)
                                                                          at android.view.ViewGroup.<init>(ViewGroup.java:579)
                                                                          at android.view.ViewGroup.<init>(ViewGroup.java:575)
                                                                          at android.support.v7.widget.LinearLayoutCompat.<init>(LinearLayoutCompat.java:153)
                                                                          at android.support.design.internal.ForegroundLinearLayout.<init>(ForegroundLinearLayout.java:61)
                                                                          at android.support.design.internal.NavigationMenuItemView.<init>(NavigationMenuItemView.java:94)
                                                                          at android.support.design.internal.NavigationMenuItemView.<init>(NavigationMenuItemView.java:90)
                                                                            ... 68 more
  • Compile 'com.android.support:design:23.0.1' try this color on Gradle

  • And makes the famous clean + rebuild.

  • But I’m using version 26 of lib support design implementation 'com.android.support:design:26.1.0'

  • clean + rebuild did not work

  • In the line getMenuInflater(). inflate(R.menu.books, menu); I did not find your menu with menu name.xml. Instead of R.menu.books it should be activity_libros_drawer.xml not?

  • This other menu file refers to the Toolbar menu

  • your icons are xml or png? ("@drawable/ic_menu_send")

  • remove "tools:showin="navigation_view" from your menu xml,

  • Leandro I ran the project here just commenting on the Adapter and the viewpager setadapter. And it didn’t give this problem, it ran normal. Try to comment and see if it will run normally

  • @Flipnovid are in xml format. I removed "tools:showin="navigation_view" from the menu and msm so the persisitiu error

  • @Andersoncanteiro I commented on the implementation of tabs and viewpager and the error continued

  • I tested on an emulator with the Marshmallow version and it worked, but on a device with Nougat and Oreo the result was the same Exception.

  • I’ll test on 7.0

  • Leandro tested in Nougat and was also normal.

  • @Andersoncanteiro Estranto is that when I comment on the line that configures the menu in Navigationview, no Exception is generated, even with tabs.

Show 10 more comments

1 answer

2


Test in 7.1.1

In this section you can do so:

  @Override
public void onBackPressed() {

  if(navigationView.isShown()){
      drawer.closeDrawers();
  }
}

And this one:

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    drawer.closeDrawers(); //fechar o drawer


    return true;
}

  • tah.. plus what the mistake was?

  • In fact I had no mistake, the code that he posted only commented this excerpt: booksTabAdapter = new Livrostabadapter(getSupportFragmentManager(), tabLayout.getTabCount(); viewPager.setOffscreenPageLimit(3); viewPager.setAdapter(booksTabAdapter);

  • I tested in emulator with Android 7.1.1 and continues with the problem. I commented everything related to Tabs and Viewpager

  • Can you pass the project? Ai try to compile here.

  • Okay, I’ll commit and go to github

  • @Andersoncanteiro ttps://github.com/leandroprogramador/Orvil

  • Leandro did the test and it was normal, gave a problem that closed the emulator when Drawer is open. in this part both when selecting the item and in the backpress, can implement as I passed.

  • Leandro which processor of your pc? I have a friend who was having problems with this, the processor did not support instructions that the emulator needed, you have some physical device that has nougat installed ?

  • @Andersoncanteiro I tested on physical devices. Both a Motorola X Play with Android 7.1 and a Zenfone 4 with Android 8.0

  • I implemented onBackPress and onNavigationItemSelected and there was no difference.

  • I’m going to test on a physical device 7.1.1 here, the implementation I talked about is just so I don’t keep doing so many checks

  • Leandro here in the normal physical device

  • Okay, let’s go to chat

  • To conclude, the problem was in my Android Studio. When testing on another machine the app ran normally.

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.