我设置了一个 Intent 来打开一个新 Activity ,并且我想传递一个整数值。 Activity 的打开有效,但一旦我使用代码传递值,应用程序就会崩溃。
这是我的主要 Activity 代码 -
public void onFinish() {
tap1.setClickable(false);
Intent i = new Intent( Single.this, FinalScore.class);
i.putExtra("kee1", count);
startActivity(i);
使用以下代码获取值:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_score);
TextView tx = (TextView) findViewById(R.id.textView3);
tx.setText(getIntent().getExtras().getInt("kee1"));
}
最佳答案
您需要将 int
值转换为 String
才能设置为 TextView
:
您可以使用以下代码:
tx.setText(""+getIntent().getExtras().getInt("kee1"));
将 ""
添加到值的第一个,为您执行此操作。
或者您可以使用以下代码来转换:
String value = String.valueOf(getIntent().getExtras().getInt("kee1"));
tx.setText(value);
您也可以使用Integer.toString();
,正如@Duncan 在评论中提到的那样。
关于java - 使用 Intent 传递整数在 Android 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23989939/