gpt4 book ai didi

java - 将两个字符串从一个 Activity 传递到另一个 Activity

转载 作者:行者123 更新时间:2023-12-01 23:26:41 25 4
gpt4 key购买 nike

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

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

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

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

最佳答案

试试这个:

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

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

并像这样获取 Intent 文本:

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

关于java - 将两个字符串从一个 Activity 传递到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19874250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com