2015-09-23 84 views
1

我有一個開放片段活動的問題,我是新的編程Android設備,但我認爲這是非常有趣的,我想爲自己做一些應用程序。
我有一個滑動菜單樣本,它的一切工作,所以我有三個列表項目(家庭,設置,約)我想打開一個活動,當我按下其中一個這是我的mainActivity,這將是如果有人告訴我該如何完成該代碼才能打開其他活動,那該多好啊! 預先感謝您。如何從導航抽屜列表中打開片段

我的MainActivity是:

public class MainActivity extends BaseActivity { 
    FragmentStackManager fm, sliding_menu; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     setUpView(); 
     menuToggeleSetUp(savedInstanceState); 
    } 
    void setUpView() { 
     fm = new FragmentStackManager(this); 
     sliding_menu = new FragmentStackManager(this); 
     drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     fm.addFragment(new MainFragment(), R.id.main_frame, false, FragmentTransaction.TRANSIT_NONE, false); 
     sliding_menu.addFragment(new SlidingMenuFragment(), R.id.slide_fragment, false, FragmentTransaction.TRANSIT_NONE, false); 
    } 
    void menuToggeleSetUp(Bundle savedInstanceState) { 
     mDrawerToggle = new ActionBarDrawerToggle(this, drawer, 
       R.drawable.ic_drawer, // nav menu toggle icon 
       R.string.app_name 
     ) { 
      public void onDrawerClosed(View view) { 
       getSupportActionBar().setTitle(mTitle); 
           invalidateOptionsMenu(); 
      } 
      public void onDrawerOpened(View drawerView) { 
       getSupportActionBar().setTitle(mDrawerTitle); 
           invalidateOptionsMenu(); 
      } 
     }; 
     drawer.setDrawerListener(mDrawerToggle); 

     if (savedInstanceState == null) { 
        } 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
      getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
       mDrawerToggle.syncState(); 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     int id = item.getItemId(); 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
        return true; 
     } 
     if (id == R.id.action_settings) { 
      System.out.println("CLEAR SETTINGS STARTED"); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 


    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
       mDrawerToggle.onConfigurationChanged(newConfig); 
    } 
} 

感謝你的幫助 BU我想保持這一個,因爲漢堡動畫,所以我找到了,我完成了我的代碼,但得到了一個錯誤在logcat的 如果有人要幫我,這將是巨大的 因此,我所做的是:

mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 
private class DrawerItemClickListener implements ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView parent, View view, int position, long id) { 
     updateDisplay(position); 
    } 
} 
private void updateDisplay(int position) { 
    Fragment fragment = null; 
    switch (position) { 
     case 0: 
      System.out.println("ACTIVITY ONE"); 
      //fragment = new Home_Fragment(); 
      break; 
     case 1: 
      //fragment = new Settings_Fragment(); 
      System.out.println("ACTIVITY TWO"); 
      // startActivity(new Intent(this, Preferences.class)); 

       break; 
      //case 2: 
      // fragment = new TB_Fragment(); 
      //break; 
     default: 
      break; 
    } 
if (fragment != null) { 
     android.app.FragmentManager fragmentManager = getFragmentManager(); 
     fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit(); 

     // update selected item and title, then close the drawer 
     //setTitle(menutitles[position]); 
     drawer.closeDrawer(mDrawerList); 
    } else { 
     // error in creating fragment 
     Log.e("MainActivity", "Error in creating fragment"); 
    } 

的我logc在是

了java.lang.RuntimeException:無法啓動活動ComponentInfo {com.intelligent_screen/com.intelligent.screen.View.MainActivity}:顯示java.lang.NullPointerException:嘗試調用虛擬方法「無效機器人。 widget.ListView.setOnItemClickListener(android.widget.AdapterView $ OnItemClickListener)」上的空對象引用

回答