导航抽屉在单击项目后不会启动新活动

时间:2018-05-16 12:32:31

标签: android navigation-drawer

我正在尝试制作一个导航抽屉,当用户点击某个项目时,该抽屉会打开一个新活动。

我的抽屉里有3件物品,我希望他们每个人都能打开不同的活动但是当我点击物品时没有任何事情发生。

 public class MarkerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_markers);
        getWindow().getDecorView().setBackgroundColor(Color.WHITE);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().hide();

        mDrawerLayout = findViewById(R.id.drawer);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
                R.string.open_drawer, R.string.close_drawer);
        mDrawerLayout.addDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.map_button:
                Intent intent = new Intent(MarkerActivity.this, MapActivity.class);
                startActivity(intent);
                break;
            case R.id.comingsoon:
                Toast.makeText(this, "Coming soon", Toast.LENGTH_SHORT).show();
                break;
            case R.id.comingsoon2:
                Toast.makeText(this, "Coming soon", Toast.LENGTH_SHORT).show();
                break;
        }
        mDrawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }
}

我的activity_markers.xml

<android.support.v4.widget.DrawerLayout 
            android:id="@+id/activity_markers"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

                <android.support.design.widget.NavigationView
                    android:id="@+id/drawer"
                    app:headerLayout="@layout/header"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@color/white"
                    app:menu="@menu/drawermenu"
                    android:layout_gravity="start">

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

drawermenu.xml

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/map_button"
        android:title="Map"/>
    <item android:id="@+id/comingsoon"
        android:title="Coming Soon"/>
    <item android:id="@+id/comingsoon2"
        android:title="Coming Soon"/>
</menu>

2 个答案:

答案 0 :(得分:0)

导航视图和抽屉的ID不正确

试试这个:

    mDrawerLayout = findViewById(R.id.activity_markers);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
            R.string.open_drawer, R.string.close_drawer);
    mDrawerLayout.addDrawerListener(toggle);
    toggle.syncState();
    NavigationView navigationView = findViewById(R.id.drawer);
    navigationView.setNavigationItemSelectedListener(this);

答案 1 :(得分:0)

我找到了答案。你只需要粘贴这一行 navigationView.bringToFront();之后NavigationView navigationView = findViewById(R.id.your_id);