-1

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.

melissa
  • 375
  • 1
  • 8
  • 20
  • Where is the crash log? There are possible at least two scenarios that `hideImageView` gets null because there is no default value or `v` imageView is called before it's connected with `id` – Yupi Jan 28 '18 at 20:17

1 Answers1

1

I don't understand which Activity you're referring to when the app crashes. However I see some easy fixes.

First, hideImageView will only be true if score111 is greater than 3. You already know it is. So just set v to INVISIBLE.

Second,

hideImageView gives null pointer error

If you get an NullPointerException here:

hideImageView = getIntent().getExtras().getBoolean("hideImageView");

Then the Intent has changed and either getIntent() or getExtras() returns null. Simply check that neither of these things have happened.

Intent intent = getIntent();
if (intent != null && intent.getExtras() != null) {
    // Now check hideImageView

But again you don't need to check for hideImageView.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • I really don't know why was I using all the hideImageView stuff and all when setting v to Invisible was sufficient :/ Thank you so much – melissa Jan 28 '18 at 20:41