Android 带有导航图的导航抽屉不工作

Android 带有导航图的导航抽屉不工作,android,navigation-drawer,android-navigationview,android-navigation-graph,Android,Navigation Drawer,Android Navigationview,Android Navigation Graph,我尝试创建一个带有导航抽屉活动的新项目。已使用viewmodels获取自动生成的片段。单击导航菜单时,它不会导航到相应的片段 build.gradle dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.legacy:legacy

我尝试创建一个带有导航抽屉活动的新项目。已使用viewmodels获取自动生成的片段。单击导航菜单时,它不会导航到相应的片段

build.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.0.0'
    implementation 'androidx.navigation:navigation-ui:2.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
MainActivity.class

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        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();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send)
                .setDrawerLayout(drawer)
                .build();
        final NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

@Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
我用导航菜单验证了导航图中的ID,它们是相同的。
我不熟悉导航图。任何帮助都将不胜感激。

在您的
活动中\u main
布局使用

<androidx.drawerlayout.widget.DrawerLayout>

  <include/>

  <com.google.android.material.navigation.NavigationView/>

</androidx.drawerlayout.widget.DrawerLayout>

要使用
抽屉布局
,请将主要内容视图定位为第一个子视图,其宽度和高度为
匹配父视图
,无
布局
>。在主内容视图之后添加抽屉作为子视图,并适当设置布局。抽屉通常用于固定宽度的高度


谢谢你,伙计。这很有效。但这没有任何意义:/我同意你@shahal。经过几天的努力,我的NavigationView可以与NavigationUI一起工作,这对我来说很有效。但这一点在任何地方都没有提及,也没有记录在案。天哪,有人能解释为什么会这样?无论如何,非常感谢你,加布里埃尔。@FilipeBezerradeSousa我最近也遇到了类似的问题。显然,这是由于XML中的反向Z顺序。因此,如果首先定义抽屉布局,它实际上是Z顺序中的第二个。这对我来说也没有多大意义,但我想我最好指出一个术语,它引导我找到解决方案,以防其他人偶然发现这条线索。
<androidx.drawerlayout.widget.DrawerLayout>

     <com.google.android.material.navigation.NavigationView/>

     <include/>

</androidx.drawerlayout.widget.DrawerLayout>