Rhiokai
Rhiokai

Reputation: 2167

Putextras not working

I have multiple variables to pass from one activity to another.

I have this in the first activity:

public void onClick(View v) {
    switch(v.getId()){
    case R.id.bStartGame:
    Intent i = new Intent(StartScreen.this, GameScreen.class);
    Bundle extras = new Bundle();
    extras.putString("Name 0", sName0);
    extras.putString("Name 1", sName1);
    extras.putString("Name 2", sName2);
            .
            .
            .
    i.putExtras(extras);
    StartScreen.this.startActivity(i);
    finish();
    break;

In the second activity, I have this:

 Intent i = getIntent();
 Bundle extras = i.getExtras();
 String name0 = extras.getString("Name 0");

 TextView test = (TextView) findViewById(R.id.tvTEST);
 test.setText(name0);

However, the textview shows nothing when I do this. How can I fix this?

EDIT: In the first activity I have:

 name0 = (EditText) findViewById(R.id.etName0);
 sName0 = name0.getText().toString();

and the same for all the other names with their relevant references.

Also, just for clarification, name0 is the edittext, sName0 is the string and "Name 0" is the key.

Upvotes: 2

Views: 120

Answers (2)

Lieuwe
Lieuwe

Reputation: 1840

In the code you have shown sNameXX have not been declared or initialised. You send them to the other Activity correctly.

Upvotes: 0

Cat
Cat

Reputation: 67512

Nothing is wrong in the code you've shown.

String name0 = extras.getString("Name 1");

name0 here has the correct value: "". If it was not sending the extras properly, it would be null, and would give you a NullPointerException on .setText().

extras.putString("Name 1", sName0);
extras.putString("Name 1", sName1);

Here, you're overwriting "Name 1" with the value of sName1, which is probably blank. I'd assume that you want to send sName0 instead.

Upvotes: 4

Related Questions