android Fragment java.lang.nullpointerexception

时间:2016-07-18 10:54:59

标签: android nullpointerexception fragment appcompatactivity

我试图在导航抽屉中选择的片段中显示不同的布局,以下是我的代码

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$1.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$1.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)

任何人都可以帮助我解决这个问题

1 个答案:

答案 0 :(得分:0)

您应该检查fragmentClass是否为null:

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();
        }


    }
}