0

我正在为 android 开发 tic tac toe 应用程序。在两个玩家部分,我创建了一个活动,要求两个玩家输入他们的名字。为此,我使用了两个EditTexts. 问题是我的应用程序在开始下一个活动时关闭。这是我的代码:

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

这是活动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);
}

这段代码没有给我任何错误,但是当我运行它时我的应用程序强制关闭。请帮我。提前致谢。

4

6 回答 6

3

尝试这个:

活动一:

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

您在导致错误的onclick之前调用了此方法。

并像这样获取意图文本:

//活动2

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);
}
于 2013-11-09T09:12:22.883 回答
0
String intArray[] = {"one","two"};
Intent in = new Intent(this, B.class);
in.putExtra("my_array", intArray);
startActivity(in);

阅读:

Bundle extras = getIntent().getExtras();
String[] arrayInB = extras.getStringArray("my_array");
于 2013-11-09T09:10:46.090 回答
0

把你的方法放在方法getIntent里面onCreate

//活动2

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);
}
于 2013-11-09T09:12:05.803 回答
0

移动Intent startbuttonintent = getIntent();到您的onCreate()方法内部

getIntent()是 中的一个方法Activity,因此您不能在静态类级别初始化中调用它。您必须仅在Activity对象准备好后调用它,例如在onCreate()方法中时

于 2013-11-09T09:12:23.487 回答
0

@Chinmay Dabke 请获取按钮“startbutton”的ID

通过使用

startbutton = (Button)findviewbyId(R.id.startbutton);
于 2013-11-09T09:15:52.260 回答
0

试试这个....在Activity1

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

Activity2

Bundle b = getArguments();
String player1name = b.getString("PLAYER1NAME");
String player2name = b.getString("PLAYER2NAME");
于 2013-11-09T09:17:22.250 回答