android 片段 java.lang.nullpointerexception

android Fragment java.lang.nullpointerexception

我正在尝试根据从导航抽屉中选择的内容在 Fragment 中显示不同的布局,以下是我的代码

public class MainActivity extends AppCompatActivity {
    private DrawerLayout drawerLayout;
    private Toolbar toolbar;
    private Menu menu;
    LinearLayout ll;

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    ll = (LinearLayout) findViewById(R.id.scrollContainerView);

    setSupportActionBar(toolbar);
    initNavigationDrawer();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    this.menu = menu;

    getMenuInflater().inflate(R.menu.menu_main, menu);

    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    MenuItem item = menu.findItem(R.id.action_search);
    item.setVisible(true);
    item.setEnabled(true);
    return true;
}

public void initNavigationDrawer() {

    NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            selectedDrawerMenu(menuItem);
            return true;
        }
    });

    View header = navigationView.getHeaderView(0);
    drawerLayout = (DrawerLayout)findViewById(R.id.drawer);

    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close) {
        @Override
        public void onDrawerClosed(View v){
            super.onDrawerClosed(v);
        }
        @Override
        public void onDrawerOpened(View v) {
            super.onDrawerOpened(v);
        }
    };

    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
}

public void selectedDrawerMenu(MenuItem menuItem) {
    Fragment fragment = null;
    Class fragmentClass = null;
    int id = menuItem.getItemId();
    switch (id){
        case R.id.home:
            fragmentClass = HomeFragment.class;
            break;

        case R.id.resource:
            fragmentClass = ResourceFragment.class;
            break;

        case R.id.survey:
            fragmentClass = SurveyFragment.class;
            break;

        case R.id.patient_data:
            fragmentClass = PatientDataFragment.class;
            break;

        case R.id.dcf:
            fragmentClass = DcfFragment.class;
            break;

        case R.id.u_guide:
            fragmentClass = UserGuideFragment.class;
            break;

        case R.id.logout:
            // close the app
    }

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

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

    drawerLayout.closeDrawers();
}

当我尝试 运行 应用程序时,出现如下异常

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)
android.support.design.widget.NavigationView.onMenuItemSelected(NavigationView.java:151)                                                            android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811) android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
android.support.design.internal.NavigationMenuPresenter.onClick(NavigationMenuPresenter.java:318)
at android.view.View.performClick(View.java:4456)
at android.view.View$PerformClick.run(View.java:18462)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
dalvik.system.NativeStart.main(Native Method)

谁能在这部分帮助我

您应该检查 fragmentClass 是否为空:

public void selectedDrawerMenu(MenuItem menuItem) {
    Fragment fragment = null;
    Class fragmentClass = null;
    int id = menuItem.getItemId();
    switch (id){
        case R.id.home:
            fragmentClass = HomeFragment.class;
            break;

        case R.id.resource:
            fragmentClass = ResourceFragment.class;
            break;

        case R.id.survey:
            fragmentClass = SurveyFragment.class;
            break;

        case R.id.patient_data:
            fragmentClass = PatientDataFragment.class;
            break;

        case R.id.dcf:
            fragmentClass = DcfFragment.class;
            break;

        case R.id.u_guide:
            fragmentClass = UserGuideFragment.class;
            break;

        case R.id.logout:
            // close the app
    }

    if(fragmentClass!=null){
        try {
            fragment = (Fragment) fragmentClass.newInstance();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();

            drawerLayout.closeDrawers();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}