我有一个错误将一个变量从一个活动发送到另一个活动。在第一个活动中,我有Inicio,Fin和Numero,当我点击按钮时,它会启动其他活动,然后我将这些值打印到TextView中。对于debbuging我评论我的代码,我尝试只打印其中一个值(inicio)。 这是我在第一个活动中的代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_examenes);
final EditText txtTemaIn=(EditText)findViewById(R.id.txtTemaIn);
final EditText txtTemaFin=(EditText)findViewById(R.id.txtTemaFin);
final EditText txtNumPregs=(EditText)findViewById(R.id.txtNumPregs);
final Button btnVerExam=(Button)findViewById(R.id.btnVerExam);
btnVerExam.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Examenes.this, Examen.class);
Bundle b=new Bundle();
b.putString("Inicio", txtTemaIn.getText().toString());
b.putString("Fin", txtTemaFin.getText().toString());
b.putString("Numero", txtNumPregs.getText().toString());
startActivity(intent);
}
});
}
在第二项活动中,我有这段代码。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_examen);
Bundle bundle = this.getIntent().getExtras();
txtTemaExa=(TextView)findViewById(R.id.txtTemaExa);
txtTemaExa.setText(bundle.getString("Inicio"));
}
答案 0 :(得分:10)
我认为您忘记了put Bundle
中的Intent
intent.putExtras(b);
用以下内容纠正:
Intent intent = new Intent(Examenes.this, Examen.class);
Bundle b=new Bundle();
b.putString("Inicio", txtTemaIn.getText().toString());
b.putString("Fin", txtTemaFin.getText().toString());
b.putString("Numero", txtNumPregs.getText().toString());
intent.putExtras(b);
startActivity(intent);
答案 1 :(得分:1)
请找到以下代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_examenes);
final EditText txtTemaIn=(EditText)findViewById(R.id.txtTemaIn);
final EditText txtTemaFin=(EditText)findViewById(R.id.txtTemaFin);
final EditText txtNumPregs=(EditText)findViewById(R.id.txtNumPregs);
final Button btnVerExam=(Button)findViewById(R.id.btnVerExam);
btnVerExam.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Examenes.this, Examen.class);
Bundle b=new Bundle();
b.putString("Inicio", txtTemaIn.getText().toString());
b.putString("Fin", txtTemaFin.getText().toString());
b.putString("Numero", txtNumPregs.getText().toString());
intent.putExtras(b);// here is change
startActivity(intent);
}
});
}
答案 2 :(得分:0)
更简单,你没有义务创建捆绑包,额外的意图是捆绑:
Intent intent = new Intent(Examenes.this, Examen.class);
intent.putExtra("Inicio",txtTemaIn.getText().toString());
intent.putExtra("Fin",txtTemaFin.getText().toString());
intent.putExtra("Numero",txtNumPregs.getText().toString());
startActivity(intent);
然后得到额外的东西,你可以这样得到它:
final Bundle extras = getIntent().getExtras();
txtTemaExa.setText(extras.getString("Inicio"));