我正在研究Android的tic tac toe应用程序。在Two player部分,我创建了一个活动,要求两个玩家输入他们的名字。为此,我使用了两个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);
}
此代码没有给我任何错误,但我的应用程序在我运行时关闭。 请帮我。 提前谢谢。
答案 0 :(得分:3)
试试这个:
活动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 之前调用此方法,这会导致错误。
然后像这样获取意图文本:
<强> //活性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);
}
答案 1 :(得分: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");
答案 2 :(得分: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);
}
答案 3 :(得分:0)
将Intent startbuttonintent = getIntent();
移到onCreate()
方法
getIntent()
是Activity
中的方法,因此您无法在静态类级别初始化中调用它。您必须在Activity
对象准备就绪后调用它,例如在onCreate()
方法中
答案 4 :(得分:0)
@Chinmay Dabke请获取“startbutton”按钮的ID
使用
startbutton = (Button)findviewbyId(R.id.startbutton);
答案 5 :(得分: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");