我正在尝试在两个活动之间传递一个整数。
活动1:
Intent intent = new Intent(this, Activity2.class);
int num1=40;
intent.putExtra("num1", num1);
startActivity(intent);
活动2:
Intent intent = getIntent();
int num = intent.getIntExtra("num1", 1);
TextView tv = (TextView) findViewById(R.id.tb_01);
tv.setText(num);
当我启动应用程序时,按下启动方法的按钮(1.代码块),它崩溃并说:“不幸的是,程序已停止。”
当我发送字符串时,它工作正常。
最佳答案
没有stacktrace这只是一个猜测,但我敢打赌这是问题所在
tv.setText(num);
在这里,您尝试将数字用作不存在的资源ID,因此,如果要在文本视图中将数字显示为文本,则需要
tv.setText(String.valueOf(num))
这将为您提供您传递的数字的字符串