Error in Navigation Drawer

时光毁灭记忆、已成空白 提交于 2019-12-11 06:18:56

问题


Somehow I am getting a null pointer Exception in this method

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    actionBarToggle.syncState();
}

actionBarToggle.syncState() in this there is a null pointer exception.

Now if I comment out this then there is null pointer exception when i touch the actionbar to open navigation drawer

    if (actionBarToggle.onOptionsItemSelected(item)) {
        return true;
    }

回答1:


I found the problem actually I was doing this

        actionBarToggle = new ActionBarDrawerToggle(this, drawerLayout,
            R.drawable.ic_drawer, R.string.drawerOpen, R.string.drawerClose) {
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle("Close");
            ActivityCompat.invalidateOptionsMenu(activity);
        }

        public void onDrawerOpened(View main) {
            getSupportActionBar().setTitle("Open");
            ActivityCompat.invalidateOptionsMenu(activity);
        }
    };
    drawerLayout.setDrawerListener(actionBarToggle); 
    drawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);//This should be    before actionBarToggle

So I was intailizing the drawerLayout after using it in actionBarToggle.




回答2:


Follow the instructions here.

http://developer.android.com/training/implementing-navigation/nav-drawer.html

Your actionBarToggle object is null. Create the ActionBarDrawerToggle and assign it to your variable before you try to use it.




回答3:


I also had NullPointerException. That was a silly mistake, I was doing:

drawerToggle = new ActionBarDrawerToggle(this,
            drawerLayout,
            R.drawable.ic_drawer,
            1,
            2){ ... }

I hardcoded those two strings for accesibility by saying 1,2, because I found those strings unimportant. As far as I was running Android 2.3-4.3 everything was ok, but on 4.4 I was getting an exception. Solution is simple: just create those two strings in values/strings.xml

Sorry for my english :)



来源:https://stackoverflow.com/questions/19344486/error-in-navigation-drawer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!