当我进行调试时,我可以看到第一个活动发送正确的数字,但第二个活动总是得到一个空值。谢谢你的帮助。
第一次活动:
val intent = Intent(this@InfBateria,ControlActivity::class.java)
intent.putExtra(EXTRA_PERCENTAGE,batteryPercentage.toString())
第二项活动:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.control_layout)
m_address = intent.getStringExtra(Connect.EXTRA_ADDRESS)
val m_percentage: String? = intent.getStringExtra(InfBateria.EXTRA_PERCENTAGE)
textView9.text = m_percentage
[编辑] 实际打开 da 第二个活动的活动代码(与第一个不同(:
val intent = Intent(this,ControlActivity::class.java)
intent.putExtra(EXTRA_ADDRESS,address)
startActivity(intent)
如果你不对这个结果意图做任何事情,它不会做任何事情:
val intent = Intent(this@InfBateria,ControlActivity::class.java)
intent.putExtra(EXTRA_PERCENTAGE,batteryPercentage.toString())
您需要更新要发送到第二个活动的意图,如下所示:
val intent = Intent(this, ControlActivity::class.java)
intent.putExtra(EXTRA_ADDRESS, address)
intent.putExtra(EXTRA_PERCENTAGE, batteryPercentage.toString())
startActivity(intent)
然后,您的接收活动将获得两个附加功能。
如果你有三个活动,并且想要从第一个到第三个获取数据,你可以这样做:
第一次活动:
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra(ThirdActivity.EXTRA_PERCENTAGE, batteryPercentage.toString())
startActivity(intent)
第二项活动:
val intent = Intent(this, ThirdActivity::class.java)
intent.putExtra(ThirdActivity.EXTRA_ADDRESS, address)
// Get the data you sent to this activity and re-add it to send again
intent.putExtra(ThirdActivity.EXTRA_PERCENTAGE, getIntent().getStringExtra(ThirdActivity.EXTRA_PERCENTAGE)
startActivity(intent)
第三项活动:
m_address = intent.getStringExtra(EXTRA_ADDRESS)
m_battery = intent.getStringExtra(EXTRA_PERCENTAGE)