I am creating a navigation drawer where i have a few items added to my menu and my one button is a logout button where i want to create an intent to go to another activity class, Here is my coding:
public void selectItemDrawer(MenuItem menuItem){
Fragment myFragment = null;
Class fragmentclass;
int id = menuItem.getItemId();
switch (menuItem.getItemId()){
case R.id.Products:
fragmentclass = Prods.class;
break;
case R.id.cus:
fragmentclass = Customer.class;
break;
case R.id.calc:
fragmentclass = Calculator.class;
break;
default:
fragmentclass = Prods.class;
}
try {
myFragment = (Fragment) fragmentclass.newInstance();
}
catch (Exception e) {
e.printStackTrace();
}
if (id == R.id.nav_send) {
Intent jj = new Intent(Navigation.this,login.class);
startActivity(jj);
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.lt,myFragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mDrawer.closeDrawers();
When i run the code the application suddenly closes when i click the logout option, I get the following error:
FATAL EXCEPTION: main
Process: com.digitialninja.mohammed.curtainclub, PID: 932
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
Here is my oncreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
mDrawer = (DrawerLayout) findViewById(R.id.drawer);
mToggle = new ActionBarDrawerToggle(this,mDrawer,R.string.open,R.string.close);
mDrawer.addDrawerListener(mToggle);
NavigationView nvDrawer = (NavigationView) findViewById(R.id.nv);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setupDrawerContent(nvDrawer);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)){
return true ;
}
return super.onOptionsItemSelected(item);
}
Here is my layout:
<?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"
tools:context=".Navigation"
android:id="@+id/drawer"
android:background="@drawable/background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lt">
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/nv"
app:headerLayout="@layout/nvheader"
app:menu="@menu/drawermenu"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>