我已经搜索了很多,尝试了所有我得到的stackoverflow作为类似问题的解决方案,但没有任何帮助。 请告诉我在我的代码中我犯了什么错误或者我留下了什么。 请帮帮我。任何更正都是可以预见的。 谢谢
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
// drawer.setDrawerListener(toggle);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
listView = (ListView) findViewById(R.id.list);
adapter = new Adapter(this, list);
listView.setAdapter(adapter);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) 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.main, 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 boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id) {
case R.id.nav_camera:
//Do some thing here
// add navigation drawer item onclick method here
Intent i = new Intent(MyBookings.this, Categories.class);
startActivity(i);
break;
case R.id.nav_gallery:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.nav_slideshow:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.nav_share:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.nav_send:
//Do some thing here
// add navigation drawer item onclick method here
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return false;
}
}
答案 0 :(得分:1)
我遇到了完全相同的问题,我通过添加以下内容对其进行了修复:
navigationView.bringToFront();
对于您的情况,请在初始化NavigationView的位置后添加:
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();
答案 1 :(得分:0)
尝试更改方法的布尔值:
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id) {
case R.id.nav_camera:
//Do some thing here
// add navigation drawer item onclick method here
Intent i = new Intent(MyBookings.this, Categories.class);
startActivity(i);
break;
case R.id.nav_gallery:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.nav_slideshow:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.nav_share:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.nav_send:
//Do some thing here
// add navigation drawer item onclick method here
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
答案 2 :(得分:0)
尝试此更改::
Intent i = new Intent(MyBookings.this, Categories.class);
startActivity(i);
finish();
答案 3 :(得分:0)
只需从Switch语句的末尾删除这两行
即可DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
让我知道是否有帮助
答案 4 :(得分:0)
更改return true。并在switch语句之前添加导航抽屉行
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
int id = item.getItemId();
switch (id) {
case R.id.nav_camera:
//Do some thing here
// add navigation drawer item onclick method here
Intent i = new Intent(MyBookings.this, Categories.class);
startActivity(i);
break;
case R.id.nav_gallery:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.nav_slideshow:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.nav_share:
//Do some thing here
// add navigation drawer item onclick method here
break;
case R.id.nav_send:
//Do some thing here
// add navigation drawer item onclick method here
break;
}
return true;
}
}