-2

In one of my activities I have a button, when pressed it stores a string value inside a bundle that I want to send to another activity and display in a TextView.

Code for when the bundle is created:

public void enemy_seen(View view){

    Intent send_enemy = new Intent(rear_gunner.this, pilot.class);
    String sight = "ENEMY SPOTTED";

    Bundle spotted = new Bundle();
    spotted.putString("TAG",sight);
    send_enemy.putExtras(spotted);



}

This code hapens on the button clicked and so far, from what I can tell this works....I believe.

When the bundle is called in second activity:

public class pilot extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pilot);
    //sets screen orientation on created
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    Bundle bundle = getIntent().getExtras();
    String something = bundle.getString("TAG");
    TextView enemy = (TextView) findViewById(R.id.enemy_spotted);
    enemy.setText(something);

}
}

The activity loads and crashes. So it must be something to do with when using the bundle I believe?

enter image description here

George Brooks
  • 199
  • 2
  • 17

1 Answers1

1

I don't see you starting the activity from the intent you set the bundle.

The activity will only receive the bundle you put in an intent if you fire that activity with that intent.

You should do a startActivity(send_enemy) after setting the bundle to the intent.

Eduardo Herzer
  • 2,033
  • 21
  • 24