问题描述
- 从Activity A 传递到 Activity B 的值返回 null
-
我想从activity B中获取信息,然后传到activity A 中,问题是我获得的值是null。我的代码显示是你点击过多少次按钮然后把值返回到第一个Activity,的确应该是这个样子的。
我贴出代码,大家看看哪里出错了?returned.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Next.this, Hw3Activity.class); intent.putExtra("text", counted.getText().toString()); startActivity(intent); /*Next is the current activity, Counted is the name of my text box*/ } }); click.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { counter++; }
这是我想把信息传递到的 Activity:
Button change; TextView text; int number; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); change = (Button) findViewById(R.id.change); text = (TextView) findViewById(R.id.count); String s1 = getIntent().getStringExtra("Textview01"); text.setText("You clicked the button " + s1 + " times."); change.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), Next.class); startActivityForResult(intent, 0); /*this button is for going to the 2nd activity,not my problem currently*/ } }); } }
解决方案
String s1 = getIntent().getStringExtra("Textview01");
应该为 :
String s1 = getIntent().getStringExtra("text");
解决方案二:
Intent intent = new Intent(getApplicationContext(), Next.class);
//添加把次数加到intent中
intent.putExtra("CLICK_NUMBER", times);
private static final int RESULT = 1;
startActivityForResult(intent, RESULT);
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT) {
//做你要做的事,解析次数
}
}
就是使用startActivityForResult
和 onActivityResult
startActivityForResult
中不能为0,
解决方案三:
String s1 = getIntent().getStringExtra("Textview01");和`intent.putExtra("text", counted.getText().toString());`
取值和传值的字符串不一样,是笔误还是问题出在这里?
时间: 2024-09-28 22:04:26