I'm using a bottom navigation menu, on each itemMenu
i'm calling a function to open the correct Activity:
//In the activty "A" where there's the bottom nav bar:
HelpActivity help = new HelpActivity();
case R.id.navigation_home:
help.openHomeActivity();
In the HelpActivity
public void openHomeActivity(){
Intent i = new Intent(getApplicationContext(), HomeActivity.class);
startActivity(i);
}
The app crashes, how to solve this, please?
the error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
HelpActivity help = new HelpActivity();
Never create an instance of an activity yourself.
Modify openHomeActivity()
to be:
public void openHomeActivity(Context context){
Intent i = new Intent(context, HomeActivity.class);
startActivity(i);
}
Then, when you call it, pass in an already existing Context
, such as the Activity
that has your bottom navigation view.