Android 如何接收包裹包裹?

Android 如何接收包裹包裹?,android,android-intent,bundle,parcelable,Android,Android Intent,Bundle,Parcelable,我创建了一个包,如下所示: val intent = Intent(classContext, Recipes::class.java) var bundle = Bundle().apply { putParcelableArrayList("LIST", ArrayList<Parcelable>(fbModel.recipeArray)) putInt("POSITION", position)

我创建了一个包,如下所示:

val intent = Intent(classContext, Recipes::class.java)

var bundle = Bundle().apply {
                    putParcelableArrayList("LIST", ArrayList<Parcelable>(fbModel.recipeArray))
                    putInt("POSITION", position)
                }

intent.putExtra("bundle", bundle)

//CHECK TO SEE IF DATA IS STORED
var passedIntent = intent.extras
var bundle2: Bundle = passedIntent.getBundle("bundle")
var recipeArray: ArrayList<RecipeTemplate> = bundle2.getParcelableArrayList("LIST")

Log.d("TAGC", " " + recipeArray[0].recipeHeader) //SUCCESS!
Log.d("TAGC", " " + position)                    //SUCCESS!


startActivity(intent)
@SuppressLint("ParcelCreator")
@Parcelize
class RecipeTemplate: Parcelable {
    var recipeHeader: String? = null
    var recipeText: String? = null
    var recipeImage: String? = null
    var recipeKey: String? = null
}
到目前为止还不错。但是,当我在另一个活动中收到bundle时,由于某种原因,它返回null,即使我使用了与上面相同的代码(测试代码以查看bundle是否存储了正确的数据)。这是接收活动:

var passedIntent: Bundle = intent.extras
var bundle = passedIntent.getBundle("bundle")
var counter: Int = bundle.getInt("POSITION", 0)
var recipeArray: ArrayList<RecipeTemplate> = bundle.getParcelableArrayList("LIST")

Log.d("TAGA", "PASSED " + counter) //SUCCESS
Log.d("TAGA", "PASSED " + recipeArray[0].recipeHeader) //FAIL: null
var passedIntent:Bundle=intent.extras
var bundle=passedIntent.getBundle(“bundle”)
变量计数器:Int=bundle.getInt(“位置”,0)
var reciparray:ArrayList=bundle.getParcelableArrayList(“列表”)
Log.d(“TAGA”,“通过”+计数器)//成功
Log.d(“TAGA”,“PASSED”+recipeArray[0]。recipeHeader)//失败:null
计数器/位置变量返回正确的数据,但由于某种原因,
reciparray
为空。同样,它在之前的活动中起作用,所以我不明白为什么这次会有不同。。。有什么想法吗

更新
如果我将光标悬停在类中的变量上,它会显示:
属性未序列化为地块
。听起来事情不像我想的那样。。。给出了什么?

尝试重构RecipeTemplate,以接受属性作为构造函数中的参数

@SuppressLint("ParcelCreator")
@Parcelize
class RecipeTemplate (
    var recipeHeader: String? = null,
    var recipeText: String? = null,
    var recipeImage: String? = null,
    var recipeKey: String? = null
) : Parcelable

问题可能在于实施parcelize的方式。我找不到任何与此相关的文档,但createFromParcel很可能只调用主构造函数。这仍然是实验性的,将来可能会改变。虽然我可能错了,但我很高兴被纠正。

你确定你的食谱图像不是空的吗?因为在第一个活动中,您正在检查配方标题。在第二张图上,你正在检查图像。哦,那只是我在尝试不同的对象变量。我肯定它不是空的。我会更新这个问题。谢谢你指出:)我会尝试提出一个答案。请确认它是否有效:)您是否刚刚添加了
:Parcelable
?我试过了,但它说,
需要一个顶级声明
我移动了括号内的所有属性(你的是大括号),并移动了最后的接口。还添加了逗号,因为它们已经是参数dude,它起作用了!真棒。非常感谢你抽出时间来帮助我你认为你能详细说明一下我刚才做的事情吗?这与Kotlin如何实现Parcelize有关。在以前的类定义中,所有内容都声明为属性。因此,当使用默认构造函数实例化类时,必须手动初始化属性。我认为Parcelize只调用主构造函数,因此,它可以实例化类,但不能设置属性值。这就是为什么一切都是空的。要解决这个问题,您必须将属性设置为主构造函数参数,以便parcelize可以设置它们的值。实现和规范在将来可能会发生变化,所以要小心。