0

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.

5
  • use putExtra to Intent
    – KOTIOS
    Nov 9, 2013 at 9:09
  • Why cant you pass HashMap or Array Nov 9, 2013 at 9:10
  • @MT8 I didn't get you. Nov 9, 2013 at 9:10
  • @ChinmayDabke please have a look at my answer !!!!!!!!!
    – GOLDEE
    Nov 9, 2013 at 9:16
  • try this way getIntent().getStringExtra("PLAYER1NAME"); getIntent().getStringExtra("PLAYER2NAME");
    – Harshid
    Nov 9, 2013 at 9:18

6 Answers 6

3

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);
}
0
0
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");
0
0

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);
}
0

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

0

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

by using

startbutton = (Button)findviewbyId(R.id.startbutton);
2
  • Yes I've done that. Forgot to add it in my question. Nov 9, 2013 at 9:17
  • can you please provide the Log getting printed while its force close
    – GOLDEE
    Nov 9, 2013 at 9:19
0

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");
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.