2015-04-06 228 views
0

DrawerActivity.java無法打開抽屜式導航

public class DrawerActivity extends MainActivity{ 
     private DrawerLayout mDrawerLayout; 
     private ListView mDrawerList; 
     private ActionBarDrawerToggle mDrawerToggle; 

     private CharSequence mDrawerTitle; 
     private CharSequence mTitle; 
     private String[] navMenuItems; 



     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.nav_drawer_menu); 

      mTitle = mDrawerTitle = getTitle(); 
      navMenuItems = getResources().getStringArray(R.array.nav_drawer_items_array); 
      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
      mDrawerList = (ListView) findViewById(R.id.left_drawer); 

      // set a custom shadow that overlays the main content when the drawer opens 
      mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 
      // set up the drawer's list view with items and click listener 
      mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, navMenuItems)); 
      //mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

      // enable ActionBar app icon to behave as action to toggle nav drawer 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
      getSupportActionBar().setHomeButtonEnabled(true); 

      // ActionBarDrawerToggle ties together the the proper interactions 
      // between the sliding drawer and the action bar app icon 
      mDrawerToggle = new ActionBarDrawerToggle(
        this,     /* host Activity */ 
        mDrawerLayout,   /* DrawerLayout object */ 
        R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ 
        R.string.drawer_open, /* "open drawer" description for accessibility */ 
        R.string.drawer_close /* "close drawer" description for accessibility */ 
      ) { 
       public void onDrawerClosed(View view) { 
        getSupportActionBar().setTitle(R.string.app_name); 
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
       } 

       public void onDrawerOpened(View drawerView) { 
        getSupportActionBar().setTitle("Menu"); 
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
       } 
      }; 
      mDrawerLayout.setDrawerListener(mDrawerToggle); 


     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.menu_main, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      // The action bar home/up action should open or close the drawer. 
      // ActionBarDrawerToggle will take care of this. 
      if (mDrawerToggle.onOptionsItemSelected(item)) { 
       return true; 
      } 

      return super.onOptionsItemSelected(item); 
     } 


     /** 
     * When using the ActionBarDrawerToggle, you must call it during 
     * onPostCreate() and onConfigurationChanged()... 
     */ 

     @Override 
     protected void onPostCreate(Bundle savedInstanceState) { 
      super.onPostCreate(savedInstanceState); 
      // Sync the toggle state after onRestoreInstanceState has occurred. 
      mDrawerToggle.syncState(); 
     } 

     @Override 
     public void onConfigurationChanged(Configuration newConfig) { 
      super.onConfigurationChanged(newConfig); 
      // Pass any configuration change to the drawer toggls 
      mDrawerToggle.onConfigurationChanged(newConfig); 
     } 
    } 

HomePage.java

public class HomePage extends DrawerActivity { 

     public static boolean active = false; 
     private Connection con; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.layout_home); 

      /* 
      try { 
       con = new ConnectDataBase().getCon(); 
       if (con == null) 
        Toast.makeText(getApplicationContext(), "con is null", Toast.LENGTH_LONG).show(); 
       con.close(); 
      }catch (Exception e){ 
       e.printStackTrace(); 
      } 
      */ 

     } 

當我在Android清單的主要活動是DrawerActivity,按下應用程序圖標,當抽屜出來。但它不會出現在HomePage活動中,並且沒有錯誤。怎麼修?!

+0

對於DrawerActivity你給出一個佈局一樣(的菜單)和HomeActivity您給出不同的佈局(沒有菜單),那麼它將如何顯示這兩個活動的菜單,所以根據它的佈局,它會顯示菜單 – Hanuman 2015-04-06 12:11:26

+0

我可以知道爲什麼你的DrawActivity擴展了MainActivity嗎?或者你在MainActivity中做了什麼代碼? – 2015-04-06 12:28:36

+0

@AndroidWeblineindia只是關於行動欄,現在我已經把它們放在一起 – Jerryc 2015-04-06 13:44:43

回答

1

它是因爲您在超類中使用了ActionBarDrawerTogglemDrawerList作爲private,因此它們不會被您的子類繼承!讓他們public,然後嘗試。

編輯

使用這條線在你的子類

super.onCreate(savedInstanceState); 
setContentView(R.layout.layout_home); 
super.onCreateDrawer(); // this line 

看看這個工程

+0

仍然無法正常工作,是所有這些都需要公開嗎? – Jerryc 2015-04-06 13:42:50

+0

是的,他們都需要公開 – 2015-04-06 13:45:55

+0

我改變了,仍然不能修復 – Jerryc 2015-04-06 13:55:13