0

I have an activity, which is a NavigationDrawer Activity and am having another activity extend this activity. When I start this child activity, I get the famous java.lang.NullPointerException

 Caused by: java.lang.NullPointerException
        at com.testapp.ShopActivity.onCreate(ShopActivity.java:23)

I don't see why this is happening since I have done everything the right way in my onCreate() method.My full ShopActivity class code:

public class ShopActivity extends BaseDrawerActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    Button navButton = (Button)findViewById(R.id.nav);

   navButton.setOnClickListener(new View.OnClickListener() { //<-Line 23
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), BaseDrawerActivity.class);
            startActivity(intent);
        }
    });

}

 @Override
public int getLayoutResourceId() {
    return R.layout.activity_shop;
}

}

Not sure why this is happening.Without the button, ShopActivity works fine and has a navigation drawer, same as the one in BaseDrawerActivity. I have tried implementing OnClickListener but I get the same error

EDIT

This is the content of the oncreate() method of the BaseDrawerActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_base_drawer);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

if I change setContentView(R.layout.activity_base_drawer); I start getting the same error on navigationDrawerFragment

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132

2 Answers2

0

navButton is null. You're never loading a layout. Or maybe you're doing it in BaseDrawerActivity but then your button isn't in the layout or has a different id.

ci_
  • 8,594
  • 10
  • 39
  • 63
  • But I have a method that loads my layout.If I add `setContentView` to my onCreate method, the app crashes.To load the layout, am using `public int getLayoutResourceId() { return R.layout.activity_shop; }` – Ojonugwa Jude Ochalifu Mar 07 '15 at 21:36
  • Done that.If i use `setContentVIew(R.layout.activity_shop)` it crashes when I start the activity with `No view found for id...` – Ojonugwa Jude Ochalifu Mar 07 '15 at 21:45
  • Please read [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) which is likely to help you determine if your object is null. – mins Mar 07 '15 at 22:30
0

As other people mentioned your navButton is null. The most likely cause is that you are not calling setContentView(). Unless in the onCreate method your super calss of BaseDrawerActivity you have setContentView(getLayoutResourceId()), this is the reason.

To solve this problem, you need show the onCreate method of your super class BaseDrawerActivity. Without this, not much further can be said.

Also, post the content of activity_shop.xml.

Kasra
  • 3,045
  • 3
  • 17
  • 34