0

I'm trying here to create a menu, that passes from a fragment to another on click. When I press the specific item(activity) in the menu the activity exits, and the faults are

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();// this line has the problem

This is my complete logcat:

FATAL EXCEPTION: main                                                                               java.lang.NullPointerException                                        

at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:414) at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:449) at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:441) atbr.exemplozxingintegration.MainActivity.selectDrawerItem(MainActivity.java:84 atbr.exemplozxingintegration.MainActivity$1.onNavigationItemSelected(MainActivity.java:50) at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:150) at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811) at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958) at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:308) at android.view.View.performClick(View.java:4439) at android.view.View$PerformClick.run(View.java:18395) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:5319) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) at dalvik.system.NativeStart.main(Native Method)

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        txResult = (TextView) findViewById(R.id.txResult);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        nvDrawer = (NavigationView) findViewById (R.id.nvView);
        setupDrawerContent(nvDrawer);}



    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        selectDrawerItem(menuItem);
                        return true;
                    }
                });
    }

    public void selectDrawerItem(MenuItem menuItem) {
        // Create a new fragment and specify the planet to show based on
        // position
        Fragment fragment = null;

        Class fragmentClass;
        switch(menuItem.getItemId()) {
            case R.id.nav_first_fragment:
                fragmentClass = MainActivity.class;
                break;
            case R.id.nav_second_fragment:
                fragmentClass = SecondActivity.class;
                break;
            case R.id.nav_third_fragment:
                fragmentClass = ThirdActivity.class;
                break;
            default:
                fragmentClass = MainActivity.class;
        }

        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

        // Highlight the selected item, update the title, and close the drawer
        menuItem.setChecked(true);
        setTitle(menuItem.getTitle());
        mDrawer.closeDrawers();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawer.openDrawer(GravityCompat.START);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

   // @Override
   // protected void onPostCreate(Bundle savedInstanceState) {
   //     super.onPostCreate(savedInstanceState);
   // 

SecondActivity.java

 private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        selectDrawerItem(menuItem);
                        return true;
                    }
                });
    }

    public void selectDrawerItem(MenuItem menuItem) {
        // Create a new fragment and specify the planet to show based on
        // position
        Fragment fragment = null;

        Class fragmentClass;
        switch(menuItem.getItemId()) {
            case R.id.nav_first_fragment:
                fragmentClass = MainActivity.class;
                break;
            case R.id.nav_second_fragment:
                fragmentClass = SecondActivity.class;
                break;
            case R.id.nav_third_fragment:
                fragmentClass = ThirdActivity.class;
                break;
            default:
                fragmentClass = SecondActivity.class;
        }

        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

        // Highlight the selected item, update the title, and close the drawer
        menuItem.setChecked(true);
        setTitle(menuItem.getTitle());
        mDrawer.closeDrawers();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawer.openDrawer(GravityCompat.START);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    // @Override
    // protected void onPostCreate(Bundle savedInstanceState) {
    //     super.onPostCreate(savedInstanceState);
    // }
user3026270
  • 187
  • 1
  • 2
  • 9

2 Answers2

0

Your Question is not clear as you have not posted your complete class code, and what error you are getting.

What i have understand is that you want to change fragment when an item in Navigation Drawer is selected. Similar to Gmail app, when a user move from inbox folder to sent folder or any other.

If you are trying to do same, you need to make one Activity, insert a fragment. When an item in navigation drawer is selected, the current fragment should be replaced by the new fragment according to the option selected in navigation drawer.

Check the following two links, that will give you the whole idea.

Link1: https://androidbelieve.com/material-navigation-drawer-with-header/

Link2: http://joerichard.net/android/android-how-to-change-fragments-when-navigation-drawer-item-selected/

Muhammad Ibrahim
  • 289
  • 2
  • 12
0

FragmentTransaction.replace calls remove(Fragment) and then add(int, Fragment, String) to actually replace the fragment. So in your case it might occur that there is no fragment in the container view and hence you are calling remove(null). So check if you have to add or replace a fragment.

you can check if there is any fragment in the container view and proceed accordingly:

 FragmentManager fragmentManager = getSupportFragmentManager();
 android.support.v4.app.Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.flContent);
 if(currentFragment==null){ 
    fragmentManager.beginTransaction().add(R.id.flContent, fragment).commit();
 }else{
   fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
 }

Hope this will solve your problem.

Rohit Arya
  • 6,751
  • 1
  • 26
  • 40