问题描述
我只想开始一个活动并将一些数据发送到第二个活动,这是我的代码:
I want to simply start an activity and send some data to my second activity , this is my code :
主要活动:
val intent = Intent(this@MainActivity, Cards::class.java)
intent.putExtra("title", catItem.name)
intent.putExtra("catId", catItem.id)
startActivity(intent)
catItem不为null,并且其中的每个项目都具有值,我已经调试了,并且对此很确定.
catItem is not null and every item in it has value , i've debugged and I'm sure about it .
我需要获取数据的第二个活动:
the second activity that I need to get data :
val bl:Bundle=intent.extras
catId=bl.getString("catId")
title=bl.getString("title")
它在第二行崩溃:
bl.getString("catId") must not be null
我调试了,捆绑包完全是空的.
I debugged and the bundle is completely empty .
此代码有什么问题?
推荐答案
要检索第二个活动的数据,您只需要直接访问intent的额外数据,如下所示:
To retrieve data on second activity you just need to access directly intent's extra data as follows:
val catId = intent.getStringExtra("catId")
此外,请确保"catId"类型为String(ID通常为Integer或Long),因为如果它不是String,则会出现相同的错误.
Also, be sure that "catId" type is String (an ID usually is an Integer or Long), because if it is not an String, you will get the same error.
这篇关于Android Kotlin-开始活动并从意图中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!