Chinmay Dabke
Chinmay Dabke

Reputation: 5140

pass two strings from one activity to another

I am working on a tic tac toe application for android. In the Two player section, I've created an activity which will ask the two players to enter their names. For that I've used two EditTexts. The problem is That my app force closes while starting the next activity. Here is my code:

//Activity 1:
    EditText player1field,player2field;
    Button startbutton;
    Intent startbuttonintent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_options);
    setupActionBar();
    player1field = (EditText) findViewById(R.id.player1field);
    player2field = (EditText) findViewById(R.id.player2field);
    startbuttonintent = new Intent(this, Activity2.class);
    startbutton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {

            String player1name = player1field.getText().toString();
            String player2name = player2field.getText().toString();
            startbuttonintent.putExtra("PLAYER1NAME",player1name);
            startbuttonintent.putExtra("PLAYER2NAME",player2name);
            startActivity(startbuttonintent);
        }
    });
}

this is activity 2

//Activity2
Intent startbuttonintent = getIntent();
TextView p1name,p2name;
    public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_3m);

    String player1name = startbuttonintent.getStringExtra("PLAYER1NAME");
    String player2name = startbuttonintent.getStringExtra("PLAYER2NAME");
    p1name = (TextView) findViewById(R.id.p1name);
    p2name = (TextView) findViewById(R.id.p2name);
    p1name.setText(player1name);
    p2name.setText(player2name);
}

This code is not giving me any errors but my app force closes when I run it. Please help me. Thanks in advance.

Upvotes: 0

Views: 7120

Answers (6)

Avijit
Avijit

Reputation: 3824

Try this:

Activity 1:

    EditText player1field,player2field;
    Button startbutton;
    Intent startbuttonintent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.single_options);
    setupActionBar();
    player1field = (EditText) findViewById(R.id.player1field);
    player2field = (EditText) findViewById(R.id.player2field);        
    startbutton.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {

            String player1name = player1field.getText().toString();
            String player2name = player2field.getText().toString();
            startbuttonintent = new Intent(this, Activity2.class);
            startbuttonintent.putExtra("PLAYER1NAME",player1name);
            startbuttonintent.putExtra("PLAYER2NAME",player2name);
            startActivity(startbuttonintent);
        }
    });
}

You have call this method before onclick which is causing error.

And fetch the intent text like this:

//Activity2

TextView p1name,p2name;
  public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_3m);
  Bundle extras = getIntent().getExtras();
  String player1name = extras.getString("PLAYER1NAME");
  String player2name = extras.getString("PLAYER2NAME");
  p1name = (TextView) findViewById(R.id.p1name);
  p2name = (TextView) findViewById(R.id.p2name);
  p1name.setText(player1name);
  p2name.setText(player2name);
}

Upvotes: 3

Abhishek Patel
Abhishek Patel

Reputation: 4328

Try This....in Activity1

Bundle bundle = new Bundle();
bundle .putString("PLAYER1NAME",player1name);
bundle .putString("PLAYER2NAME",player2name);
intent1.putExtras(bundle);

in Activity2

Bundle b = getArguments();
String player1name = b.getString("PLAYER1NAME");
String player2name = b.getString("PLAYER2NAME");

Upvotes: 0

GOLDEE
GOLDEE

Reputation: 2318

@Chinmay Dabke Please get the id of the button "startbutton"

by using

startbutton = (Button)findviewbyId(R.id.startbutton);

Upvotes: 0

Raghav Sood
Raghav Sood

Reputation: 82543

Move Intent startbuttonintent = getIntent(); to inside your onCreate() method

getIntent() is a method in Activity, so you can't call it in a static class level initialization. You must call it only after your Activity object is ready, like when in the onCreate() method

Upvotes: 0

Devrim
Devrim

Reputation: 15533

Take yout getIntent method inside onCreate method

//Activity2

TextView p1name,p2name;
    public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_3m);
    Intent startbuttonintent = getIntent();

    String player1name = startbuttonintent.getStringExtra("PLAYER1NAME");
    String player2name = startbuttonintent.getStringExtra("PLAYER2NAME");
    p1name = (TextView) findViewById(R.id.p1name);
    p2name = (TextView) findViewById(R.id.p2name);
    p1name.setText(player1name);
    p2name.setText(player2name);
}

Upvotes: 0

KOTIOS
KOTIOS

Reputation: 11194

String intArray[] = {"one","two"};
Intent in = new Intent(this, B.class);
in.putExtra("my_array", intArray);
startActivity(in);

For Reading :

Bundle extras = getIntent().getExtras();
String[] arrayInB = extras.getStringArray("my_array");

Upvotes: 0

Related Questions