我想從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");