0

I have 2 activities. Both have surfaceview implemented. In the first one I have a variable called score1, and I want to pass the value from score1 to the second activity.

Every time I run the app it crash after it finish the first activity and tries to load the second. Before I write code to grab the intent extras in the second activity everything worked well.

My first activity has this intent:

Intent intent = new Intent (myContext, PunchItActivity.class);
intent.putExtra("score_var", score1);
myContext.startActivity(intent);
((Activity) myContext).finish();

And in my second activity the following code tries to grab the value of score1 variable:

Intent mIntent = ((Activity) myContext).getIntent();
int score1 = mIntent.getIntExtra("score_var", 0);
score1 *= molesWhacked + 10;
canvas.drawText("Score: " + Integer.toString(score1), 20, 160, blackPaint);

I cannot figure why the app is crashing every time activity one leaves the scene and activity two tries to render it's surfaceview and display the values from score1.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
André Castro
  • 1,527
  • 6
  • 33
  • 60
  • 2
    Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Apr 27 '18 at 23:29
  • LogCat says: Attempt to invoke virtual method 'android.content.Intent android.app.Activity.getIntent()' on a null object reference – André Castro Apr 27 '18 at 23:35
  • That suggests that `myContext` is `null`. You might consider adding the full source to the second activity to your question, or at least enough to show where and how you are trying to set `mContext`. – CommonsWare Apr 27 '18 at 23:41

2 Answers2

0

try like this in your first activity:

Intent intent = new Intent (myContext, PunchItActivity.class);
        intent.putExtra("score_var", score1);
        myContext.startActivity(intent);
        //((Activity) myContext).finish();
ColorfulDark
  • 53
  • 1
  • 9
0

The problem is because your mContext is null, so you need to initialize it.

You don't need to use mContext if you're starting an Activity. You can use this of your activity, something like this:

Intent intent = new Intent (this, PunchItActivity.class);
intent.putExtra("score_var", score1);
this.startActivity(intent);
this.finish();

or using something like this if your code inside an anonymous class:

Intent intent = new Intent (YourActivity.this, PunchItActivity.class);
intent.putExtra("score_var", score1);
YourActivity.this.startActivity(intent);
YourActivity.this.finish();

In your second activity, you don't need to use mContext to use the getIntent() method because you only need to get the intent which start the second activity. The intent is already attached to the second activity. So, you need to modify your code to something like this:

Intent mIntent = getIntent();
int score1 = mIntent.getIntExtra("score_var", 0);
score1 *= molesWhacked + 10;
canvas.drawText("Score: " + Integer.toString(score1), 20, 160, blackPaint);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96