error - NullpointException in an android app with DrawerLayout

时间:2015-09-01 21:43:04

标签: java android android-studio nullpointerexception drawerlayout

I was trying to create a simple material design interface in android studio. I am getting error at particular line where I am opening drawer. As soon as I click home icon in actionbar (toolbar), the app crashes.

Here is the code I tried.

public class MainActivity extends AppCompatActivity  {

public DrawerLayout mDrawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    android.support.v7.app.ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
    actionBar.setDisplayHomeAsUpEnabled(true);

    DrawerLayout mDrawerLayout;
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);

    FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Snackbar.make(findViewById(R.id.drawer_layout), "I'm a Snackbar", Snackbar.LENGTH_LONG).setAction("Action", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "Snackbar Action", Toast.LENGTH_LONG).show();
                }
            }).show();
        }
    });
}



@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) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    switch (id) {
        case android.R.id.home:
            mDrawerLayout.openDrawer(GravityCompat.START);
            return true;
        case R.id.action_settings:
            return true;
    }

    return super.onOptionsItemSelected(item);
}

}

I have tried to change the action of FAB and Home Icon, that is working !! But in normal drawer open with home icon, I get following error.

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(int)' on a null object reference
        at jani.jigar.designdemo.MainActivity.onOptionsItemSelected(MainActivity.java:67)

I have seen same cases, but they didn't initialized object. I did in the beginning. I can't find the error. Any help will be appreciated.

1 个答案:

答案 0 :(得分:2)

This is happening because you're aliasing your activity's instance variable mDrawerLayout in the onCreate method with a local variable. And since scoping in Java goes from the inside out, you're never setting the reference of mDrawerLayout outside of the method. So all you need to in order to fix your issue is to remove DrawerLayout mDrawerLayout; from onCreate.