Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to read from field 'int android.support.v4.app.Fragment.mContainerId' on a null object reference when using Navigation Drawer

Below is my StackTrace of the error that I am getting in Android Studio when navigation using my navigation drawer. This is a new error as it hasn't happened me before. So I am not sure what if anything I've changed.

08-11 23:16:37.823 15951-15951/com.example.aids.a09application
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aids.a09application, PID: 15951
java.lang.NullPointerException: Attempt to read from field 'int android.support.v4.app.Fragment.mContainerId' on a null object reference
at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1017)
at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:976)
at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

Below is the Java Class where I have my Fragment Transactions for my Navigation Drawer.

public class MainActivity extends AppCompatActivity {
    DrawerLayout drawerLayout;
    Toolbar toolbar;
    ActionBarDrawerToggle actionBarDrawerToggle;
    FragmentTransaction fragmentTransaction;
    NavigationView navigationView;
    Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);

        drawerLayout.addDrawerListener(actionBarDrawerToggle);
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.main_container, new HomeFragment());
        fragmentTransaction.commit();
        getSupportActionBar().setTitle("Drift Fan");
        navigationView = (NavigationView) findViewById(R.id.navigation_view);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.Home:
                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container, new HomeFragment());
                        fragmentTransaction.commit();
                        getSupportActionBar().setTitle("Home Fragment");
                        item.setCheckable(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.my_account:
                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container, new myAccountFragment());
                        fragmentTransaction.commit();
                        getSupportActionBar().setTitle("My Account");
                        item.setCheckable(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.nav_about:
                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container, new AboutDriftingFragment());
                        fragmentTransaction.commit();
                        getSupportActionBar().setTitle("About Drifting");
                        item.setCheckable(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.nav_shop:
                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container, new ShopFragment());
                        fragmentTransaction.commit();
                        getSupportActionBar().setTitle("Shop");
                        item.setCheckable(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.nav_news:
                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container, new NewsFragment());
                        fragmentTransaction.commit();
                        getSupportActionBar().setTitle("News");
                        item.setCheckable(true);
                        drawerLayout.closeDrawers();
                        break;
                    case R.id.nav_media:
                        intent = new Intent(MainActivity.this, media_main.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        MainActivity.this.startActivity(intent);
                        break;
                    case R.id.nav_results:
                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container, new ResultsFragment());
                        fragmentTransaction.commit();
                        getSupportActionBar().setTitle("Results");
                        item.setCheckable(true);
                        drawerLayout.closeDrawers();
                        break;

                }


                return false;
            }

        });

    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        actionBarDrawerToggle.syncState();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.drawer_menu, menu);
        return true;



    }
}
like image 798
S.Doyle Avatar asked Aug 11 '17 22:08

S.Doyle


1 Answers

I have had the same issue but when i extend from v4.app.Fragment class. instead of android.app.Fragment it is working fine without crashes.

also see this it has some solutions that you can try.

like image 97
Mihir Dave Avatar answered Oct 14 '22 11:10

Mihir Dave