点击导航抽屉后,新的活动将不会开始

时间:2018-08-24 21:12:59

标签: android android-studio android-studio-3.0

我知道这个问题存在,但是答案对我的情况不起作用。 我有一个包含四个项目的导航抽屉,但是当我单击第一个项目(我正在处理的项目)时,我创建的新活动没有打开,我已经查看并尝试了在互联网上找到的所有内容但似乎没有任何效果。 我是android studio的新手,所以我真的不知道该如何解决。我正在使用的android studio版本是3.0。 这是我的菜单xml:

<?xml version="1.0" encoding="utf-8"?>
<!--Icons made by http://www.freepik.com
from "https://www.flaticon.com
is licensed by http://creativecommons.org/licenses/by/3.0/-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/m1" android:title="Agora Mall"
      android:icon="@drawable/ic_ammunition"/>
<item android:id="@+id/m2" android:title="Downtown Center"
      android:icon="@drawable/ic_ammunition"/>
<item android:id="@+id/m3" android:title="Galeria 360"
      android:icon="@drawable/ic_ammunition"/>
<item android:id="@+id/m4" android:title="Sambil"
      android:icon="@drawable/ic_ammunition"/>
</menu>

这是主要的活动类别代码:

package com.example.arlet.storemaps;

import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.Window;

public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open,  R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

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

    switch(item.getItemId()){
        case R.id.m1:
            Intent intent = new Intent(MainActivity.this, AgoraActivity.class);
            startActivity(intent);
            return true;
    }

    return super.onOptionsItemSelected(item);
}

}

3 个答案:

答案 0 :(得分:1)

您必须为导航抽屉菜单实现onNavigationItemSelected()
onOptionsItemSelected()与工具栏选项菜单项有关。
如果您创建一个新项目并选择

,则可以找到所有必需的代码。
  

导航抽屉活动

作为MainActivity的类型。

答案 1 :(得分:0)

您可以通过以下方式处理导航抽屉项目单击:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        // This method will trigger on item Click of navigation menu
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {

            //Check to see which item was being clicked and perform appropriate action
            switch (menuItem.getItemId()) {
                //Replacing the main content with ContentFragment Which is our Inbox View;
                case R.id.m1:
                    Intent intent = new  Intent(MainActivity.this, AgoraActivity.class);
                    startActivity(intent);
                    return true;
                    break;
                case R.id.m2:

                    break;
                case R.id.m3:

                    break;
                case R.id.m4:

                    break;

            }


            menuItem.setChecked(true);

            return true;
        }
    });

答案 2 :(得分:0)

您必须实现NavigationView.OnNavigationItemSelectedListener才能使抽屉项目正常工作

将此添加到您的类声明中

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
// then overide this method to
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {        
        int id = item.getItemId();
        switch(id){
            case R.id.m1:
                Intent intent = new Intent(MainActivity.this, AgoraActivity.class);
                startActivity(intent);
                return true;
            //other cases goes here
        }
        return super.onOptionsItemSelected(item);
    }
    //you might want to ovewrite this method too
    @Override
    public void onBackPressed() {
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        } else {
        super.onBackPressed();
    }
}