2016-07-18 120 views
0

我試圖表現出片段不同的佈局按照從抽屜式導航選擇,下面是我的代碼Android的片段顯示java.lang.NullPointerException

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) 

任何人都可以請幫我在這部分

回答

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


    } 
} 
+0

沒有用仍然是相同類型的崩潰... –

+0

我做了一點改變,我認爲你有創建片段的問題。測試它並告訴我結果 –

+0

非常感謝... –

相關問題