-1

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>

2 Answers2

0

You don't have any Fragment in your Activity, so I assume you should change the following line:

fragmentManager.beginTransaction().replace(R.id.lt,myFragment).commit();

To:

fragmentManager.beginTransaction().add(R.id.lt,myFragment).commit();
gi097
  • 7,313
  • 3
  • 27
  • 49
0

Replace your code by this.

 public void selectItemDrawer(MenuItem menuItem){
        Fragment myFragment = null;
        int id = menuItem.getItemId();

        if (id == R.id.nav_send) {
            Intent jj = new Intent(Navigation.this,login.class);
            startActivity(jj);
        }
        else {
            switch (id) {
                case R.id.Products:
                    myFragment = Prods().newInstance();
                    break;
                case R.id.cus:
                    myFragment = Customer().newInstance();
                    break;
                case R.id.calc:
                    myFragment = Calculator().newInstance();
                    break;

                default:
                    myFragment = Prods().newInstance();
            }
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.lt,myFragment).commit();
        }
        mDrawer.closeDrawers();
        setTitle(menuItem.getTitle());
        menuItem.setChecked(true);
    }
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212