Heloo everyone! I have a problem with NavigationView and fragments. I created three fragments (as options in my NavigationView) when I click no matter which option there is a NullPointerException. I have no idea what is wrong :(
Here is activity_main.xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentsContainer"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context = ".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:background="#aaaaaa"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="start"
android:id="@+id/navigationView"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu"
/>
This is how I handle options:
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment=null;
Class fragmentClass=null;
int id = item.getItemId();
switch(id){
case R.id.aboutProgram:
fragmentClass = ProgramFragment.class;
break;
case R.id.aboutAutor:
fragmentClass = AuthorFragment.class;
break;
case R.id.Settings:
fragmentClass = SettingsFragment.class;
break;
default:
fragmentClass = ProgramFragment.class;
}
try{
fragment = (Fragment) fragmentClass.newInstance();
}catch(Exception e){
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragmentsContainer,fragment).commit();
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});