【问题标题】:cannot open navigation drawer无法打开导航抽屉
【发布时间】:2015-04-06 11:39:41
【问题描述】:

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 manifest 中的主要活动是 DrawerActivity 时,当按下应用程序图标时,抽屉就会出来。但它不会出现在 HomePage 活动中,也没有错误。如何解决?!

【问题讨论】:

  • 对于 DrawerActivity 你给定的一个布局(带菜单)和在 HomeActivity 你给定的不同布局(不带菜单),然后它将如何显示两个活动的菜单,因此根据布局它将显示菜单
  • 我能知道你为什么在你的 DrawerActivity 中扩展 MainActivity 吗?或者你在 MainActivity 中做了什么代码?
  • @AndroidWeblineindia 只是关于操作栏,现在我将它们放在一起
  • @Allu 那么我应该如何将抽屉代码应用于所有活动.. 我只是按照人们所说的
  • 意思是如果我想做活动,每次都需要创建抽屉布局吗? @Allu

标签: java android drawer


【解决方案1】:

这是因为您在超类中使用了ActionBarDrawerTogglemDrawerList 作为private,因此它们不会被您的子类继承!将它们设为 public ,然后尝试。

编辑

在你的子类中使用这一行

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

看看这是否有效

【讨论】:

  • 我改了,还是不能修复
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 2016-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多