我在两个String
之间发送Activities
。这是我发送字符串class
的代码。
public class SetData extends Activity implements OnClickListener{
EditText et;
Button bConfirm;
TextView tv;
String bread;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.data_input);
initiate();
bConfirm.setOnClickListener(this);
}
private void initiate() {
// TODO Auto-generated method stub
et = (EditText) findViewById(R.id.ET);
bConfirm = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.TV);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bread = et.getText().toString();
Bundle basket = new Bundle();
basket.getString("key", bread);
Intent stringSend = new Intent(SetData.this, PrintData.class);
stringSend.putExtras(basket);
startActivity(stringSend);
} }
此活动按预期启动另一项活动。另一项活动是班级:
public class PrintData extends Activity implements OnClickListener{
EditText tv;
RadioGroup RG;
Button print;
String newstring = "No content received.";
Bundle gotBasket;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.data_output);
initiate();
Intent oldintent = getIntent();
gotBasket = oldintent.getExtras();
newstring = gotBasket.getString("key");
tv.setText(newstring);
//print.setOnClickListener(this);
}
private void initiate() {
// TODO Auto-generated method stub
tv = (EditText) findViewById(R.id.TV);
print = (Button) findViewById(R.id.print);
RG = (RadioGroup) findViewById(R.id.radioGroup);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
} }
String newstring的默认值是“No content received。”。当新活动打开时,它会显示上述文本。这意味着字符串由Activity1(SetData)发送,但不由Activity2(PrintData)接收。
这可能是什么原因?我该如何纠正?
答案 0 :(得分:2)
您应该在第一项活动中basket.putString()
,而不是getString()
答案 1 :(得分:1)
在您开展的第一项活动basket.getString("key", bread);
要使用捆绑包发送数据,您应该使用basket.putString("key", bread);
答案 2 :(得分:0)
在你的第一个活动中,你在做basket.putString(“key”,bread);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bread = et.getText().toString();
Bundle basket = new Bundle();
basket.putString("key", bread);
Intent stringSend = new Intent(SetData.this, PrintData.class);
stringSend.putExtras(basket);
startActivity(stringSend);
}
或 第一项活动:
stringSend.putExtra("key", bread)
Secend Activity:获取值
newstring=getIntent().getExtras().getString("key");