[英]How to open the side navigation drawer when click on bottom menu?
This is what i'm trying to achieve 这就是我想要实现的
I want settings menu click will appear side nav drawer 我想要设置菜单单击将出现在侧面导航抽屉中
HomeActivity.java HomeActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
homeFragment = new HomeFragment();
analyticsFragment = new AnalyticsFragment();
paymentFragment = new PaymentFragment();
settingsFragment = new SettingsFragment();
drawerLayout = findViewById(R.id.drawerlayout);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main_frame,new HomeFragment()).commit();
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.navigation_home:
setFragment(homeFragment);
return true;
case R.id.navigation_analytics:
setFragment(analyticsFragment);
return true;
case R.id.navigation_payment:
setFragment(paymentFragment);
return true;
case R.id.navigation_settings:
drawerLayout.openDrawer(GravityCompat.END);
return true;
default:
return false;
}
}
});
BottomNavigationView navView = findViewById(R.id.main_nav);
navView.setItemIconTintList(null);
And this is the error i get 这是我得到的错误
Unable to start activity ComponentInfo{com.example.ewallet/com.example.ewallet.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference
EDIT : I tried to paste some of my code here but StackOverflow error too many code to post 编辑:我试图在这里粘贴一些代码,但是StackOverflow错误太多代码无法发布
You didn't bind your object correctly and caused a NullPointerException. 您没有正确绑定对象,并导致了NullPointerException。
Please paste your code and we can help you. 请粘贴您的代码,我们可以为您提供帮助。
I saw a weird part, but maybe this error will still appear, you may need to paste your layout.xml 我看到了一个奇怪的部分,但也许此错误仍会出现,您可能需要粘贴您的layout.xml
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
BottomNavigationView navView = findViewById(R.id.main_nav);
navView.setItemIconTintList(null);
Should be modify to 应该修改为
mMainNav.setItemIconTintList(null);
You can check if it is null with 您可以使用以下命令检查它是否为空
getActivity().findViewById(R.id.XXX)
UPDATE 更新
drawerLayout = (DrawerLayout)getView().findViewById(R.id.drawerlayout);
case R.id.navigation_settings:
drawerLayout.openDrawer(drawerLayout);
return true;
Took me 4 days to figure this out. 花了我4天时间解决这个问题。 Turned out you need to have drawerlayout in your activity layout.
原来,您需要在活动布局中设置抽屉式布局。 My problem was drawerlayout from different xml that's why returned null.
我的问题是来自不同xml的抽屉布局,这就是为什么返回null的原因。 Thx for those who tried for helping me
感谢那些试图帮助我的人
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
tools:openDrawer="end">
<include
layout="@layout/app_bar_settings"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_settings"
app:menu="@menu/activity_settings_drawer" />
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container"
tools:context=".HomeActivity">
<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="56dp">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/main_nav"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:labelVisibilityMode="labeled"
app:itemTextColor="@color/colorPrimaryDark"
app:menu="@menu/bottom_nav_menu" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.DrawerLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.