我在导航抽屉中的意图不起作用

my intent in navigation drawer is not working

当我点击导航抽屉中的任何项目时,抽屉会切换,而不是转到新的 activity。

主要活动:


  public class MainActivity extends AppCompatActivity {

    LinearLayoutManager mLayoutManager; //for sorting
    SharedPreferences mSharedPreferences; //for saving sort setting

    DrawerLayout drawerLayout;
    ActionBarDrawerToggle toggle;
    NavigationView navigationView;
    Toolbar toolbar;
    RecyclerView mRecyclerView;
    List<FoodData> myFoodList;
    FoodData mFoodData;
    MyAdapter myAdapter;
    SearchView searchView;
    private boolean ascending = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // setUpToolbar();
       // getSupportActionBar().hide();

        toolbar =(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //menu hooks
        drawerLayout = findViewById(R.id.drawerLayout);
        navigationView = findViewById(R.id.navigation_menu);

        toggle = new ActionBarDrawerToggle(this, drawerLayout,toolbar,R.string.open,R.string.close);
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();






        mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        searchView = findViewById(R.id.srch);

        mSharedPreferences= getSharedPreferences("SortSettings", MODE_PRIVATE);
        String mSorting =mSharedPreferences.getString("Sort","newest"); // where if no setting is selected newest will be default

        if(mSorting.equals("newest")){
            mLayoutManager = new LinearLayoutManager(this);
            //this will load the items from bottom means newest first
            mLayoutManager.setReverseLayout(true);
            mLayoutManager.setStackFromEnd(true);

        }else if(mSorting.equals("oldest")){
            mLayoutManager = new LinearLayoutManager(this);
            //this will load the items from bottom means oldest first
            mLayoutManager.setReverseLayout(false);
            mLayoutManager.setStackFromEnd(false);

        }

        //for setting company name in the navigation drawer
      //  navigationView=(NavigationView)findViewById(R.id.navigation_menu);
        //navigationView.setNavigationItemSelectedListener(this);
       // View header = navigationView.getHeaderView(0);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId())
                {
                    case  R.id.nav_favourite:
                        Intent intent = new Intent(MainActivity.this, favourite.class);
                        startActivity(intent);
                       // drawerLayout.closeDrawer(GravityCompat.START);
                        break;

                    case R.id.nav_about_us:
                        Intent intent1 = new Intent(MainActivity.this , About_us.class);

                        startActivity(intent1);
                       // drawerLayout.closeDrawer(GravityCompat.START);
                        break;
                    case R.id.nav_rate_us:
                        Intent intent2 = new Intent(MainActivity.this , RateUs.class);

                        startActivity(intent2);
                       // drawerLayout.closeDrawer(GravityCompat.START);
                        break;
                    case  R.id.nav_share:

                        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                        sharingIntent.setType("text/plain");
                        String shareBody =  "http://play.google.com/store/apps/detail?id=" + getPackageName();
                        String shareSub = "Try now";
                        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
                        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                        startActivity(Intent.createChooser(sharingIntent, "Share using"));

                    //drawerLayout.closeDrawer(GravityCompat.START);
                        break;
                    case R.id.nav_setting:
                        Intent intent3 = new Intent(MainActivity.this , SettingsActivity.class);

                        startActivity(intent3);
                       // drawerLayout.closeDrawer(GravityCompat.START);
                        break;

                }
                return true;
            }
        });



        GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this,1);
        mRecyclerView.setLayoutManager(mLayoutManager);

        myFoodList = new ArrayList<>();

        mFoodData = new FoodData("Rawa Kesar",R.drawable.rawa,"INGREDIENTS"); 
        mFoodData = new FoodData("Jalebi",R.drawable.jalebi,"INGREDIENTS"); 
        mFoodData = new FoodData("Rasmalai",R.drawable.rasmlai,"INGREDIENTS"); 
        mFoodData = new FoodData("Kaju Katli",R.drawable.kajuktli,"INGREDIENTS"); 


         myAdapter = new MyAdapter(MainActivity.this,myFoodList);
        mRecyclerView.setAdapter(myAdapter);


        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                myAdapter.getFilter().filter(s.toString());
                return false;

            }
        });


    }


//    public void btn_uploadActivity(View view) {
//
//       startActivity(new Intent(this,Upload_Recipe));
//    }

//    public void setUpToolbar() {
//        drawerLayout = findViewById(R.id.drawerLayout);
//        Toolbar toolbar = findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
//        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
//        drawerLayout.addDrawerListener(actionBarDrawerToggle);
//        actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.black));
//        actionBarDrawerToggle.syncState();
//
//    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int id=item.getItemId();

        if(id==R.id.sort){
            //display alert dialogue to choose sorting
            showSortDialogue();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showSortDialogue() {
        //options to display in dialogue
        String[] sortOptions = {"Newest","Oldest"};
        //create alert dislogue
        AlertDialog.Builder builder= new AlertDialog.Builder(this);
        builder.setTitle("Sort By").setIcon(R.drawable.sort_2).setItems(sortOptions,new  DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(which==0){
                            SharedPreferences.Editor editor=mSharedPreferences.edit();
                            editor.putString("Sort","newest");
                            editor.apply();
                            recreate();
                        }else if(which==1){
                            SharedPreferences.Editor editor=mSharedPreferences.edit();
                            editor.putString("Sort","oldest");
                            editor.apply();
                            recreate();
                        }
                    }
                }
        );
        builder.show();
    }


//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.menu,menu);
//        MenuItem menuItem=menu.findItem(R.id.search);
//        //SearchView searchView=(SearchView) menuItem.getActionView();
//        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
//            @Override
//            public boolean onQueryTextSubmit(String s) {
//                return false;
//            }
//
//            @Override
//            public boolean onQueryTextChange(String s) {
//                myAdapter.getFilter().filter(s.toString());
//                return false;
//            }
//        });
//        return super.onCreateOptionsMenu(menu);
//    }

    @Override
    public void onBackPressed() {

        Intent a =new Intent(Intent.ACTION_MAIN);
        a.addCategory(Intent.CATEGORY_HOME);
        a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(a);
    }

}

这是我的 mainActiviy.xml 代码,我在其中使用了导航视图

我检查了我的代码很多次,但没能找到错误...如果有人能帮助我,那将是很大的帮助 提前致谢。

在线性布局下方添加导航视图,它将起作用。在抽屉布局中,内容应该在前,然后是导航视图。