I have two activities lets say Level Activity and Result Activity, so what happens is when I go from level -> level1 -> result activity I check if the score is > 4 if it is I unlock the next level in level activity by simply removing an imageview on a button in level activity by doing something like this in Result Activity
SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
final int score111 = pref.getInt("score111", 0);
v1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
startActivity(new Intent(ResultActivity2.this,Lyricswho.class));
if (score111 >= 4) {
Intent act2= new Intent(ResultActivity2.this,Lyricswho.class);
act2.putExtra("hideImageView", true);
startActivity(act2);
}
}
});
}
and then in Level activity
if(score111 >=4 ){
hideImageView = getIntent().getExtras().getBoolean("hideImageView");
if (hideImageView) {
v.setVisibility(View.INVISIBLE);
}
}
It works fine for the first time but when I restart the app by closing it down and go to Level Activity it crashes and goes back to previous activity. The reason as I have figured out is the hideImageView gives null pointer error when I restart the app because it satisfies the condition that is score is greater then 4 (which previously it doesn't because the score is 0 so the code doesn't go in if condition) but there is nothing in hideImageView so it throws null exception. I tried using boolean variable so that if condition is ran only once but then my imageview appears again so I can't use that. How can I solve this problem? Been stuck there for days.