Hello guys! I'm currently working on a matching app. In the app, the user can select what mode of difficulty they want(easy, medium, hard and suvival) as well as select one of the sets of images for the GridView(nba logos or world flags). My problem is that when the user selects their preferred difficulty and image set, the app stops working. I'm getting a NullPointerException. Here's my code:
View.OnClickListener startGame = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
Bundle b = getIntent().getExtras();
Bundle d = getIntent().getExtras();
if(b != null){
String mode = b.getString("mode");
String logo = d.getString("logo");
if(mode.equals("easy") && logo.equals("nba")) {
i.setClass(getBaseContext(), PlayEasyNBA.class);
startActivity(i);
}
else if(mode.equals("medium") && logo.equals("nba")){
i.setClass(getBaseContext(), PlayMediumNBA.class);
startActivity(i);
}
else if(mode.equals("hard") && logo.equals("nba")){
i.setClass(getBaseContext(), PlayHardNBA.class);
startActivity(i);
}
}
}
};
In this activity, the user chooses which image set they want:
View.OnClickListener nba = new View.OnClickListener() {
@Override
public void onClick(View v) {
choiceLogo = "nba";
Intent i = new Intent();
i.putExtra("logo", choiceLogo);
i.setClass(getBaseContext(), Choose.class);
startActivity(i);
Toast.makeText(getApplicationContext(), "NBA Teams selected!",
Toast.LENGTH_SHORT).show();
finish();
}
};
View.OnClickListener flags = new View.OnClickListener() {
@Override
public void onClick(View v) {
choiceLogo = "flags";
Intent i = new Intent();
i.putExtra("logo", choiceLogo);
i.setClass(getBaseContext(), Choose.class);
startActivity(i);
Toast.makeText(getApplicationContext(), "Flags of the World selected!",
Toast.LENGTH_SHORT).show();
finish();
}
};
The NullPointerException is right before the if loop. The app works well if I code it this way: if(mode.equals("easy")) {
but with this, it would only display the Easy difficulty with the NBA image set.
Any advice?