我试图通过意图将一个int传递给另一个类,并设法成功通过整数,但我不确定如何将一个Bundle转换为一个Integer。
来自意图的代码:
private void nextPage()
{
Intent intent = new Intent(this, Timer.class).putExtra("totalTime", totalTime);
startActivity(intent);
}
Timer类中的代码:
Bundle time = getIntent().getExtras();
if(time == null)
{
timeDisp.setText("Failed.");
}
else
{
totalTimeMs = Integer.parseInt(String.valueOf(time));
timeDisp.setText(totalTimeMs);
}
提前致谢:)
答案 0 :(得分:0)
如果totalTime
类型为int
,您可以使用putExtra()
,则可以使用:
int time = getIntent().getExtras().getInt("totalTime");
答案 1 :(得分:0)
你没有将你的意图添加到Bundle,所以在接收活动中你试图从空Bundle中获取数据。
您可以通过以下方式将数据添加到分发包中:
private void nextPage()
{
Intent intent = new Intent(this, Timer.class);
Bundle b = new Bundle():
b.putString("totalTime", totaltime);
intent.putExtras(b);
startActivity(intent);
}
然后你将从包中检索字符串:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String time = extras.getString("totalTime");
答案 2 :(得分:0)
Intent可以直接保存所有java基元类型和parcelable / serializable对象。
你可能会感到困惑,它也可能拥有Bundles。
你真的需要把你的整数放在一个Bundle中吗?对于逻辑耦合的多个值,情况可能正确。
检查Intent API。