This is what i'm trying to achieve
I want settings menu click will appear side nav drawer
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
You didn't bind your object correctly and caused a 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
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. Turned out you need to have drawerlayout in your activity layout. My problem was drawerlayout from different xml that's why returned 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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.