下面的類裡的信息我想把它包含在intent裡面發送給一個activity。當圖片按鈕“superman”按下的時候,onClick()方法會發送intent給SuperheroActivity。但是當我試著在另一個activity裡面檢索信息的時候,得到了“false”
public class MenuActivity extends Activity implements
OnClickListener {
private ImageButton superman;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
superman = (ImageButton) findViewById(R.id.superman);
superman.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(MenuActivity.this, SuperheroActivity.class);
intent.putExtra("id", v.getId());
startActivity(intent);
}
}
SuperheroActivity:
Intent intent = getIntent();
id = intent.getIntExtra("id", 0);
// This is just a dirty way for me to see the value of the id I am getting.
TextView text = (TextView) findViewById(R.id.superheroText);
text.setText(id);
text.setText(id);這行有問題吧,int型的值不能直接賦給text.setText();正確的寫法:text.setText(id+"");