startActivityForResult putExtras 被接收为 NULL
startActivityForResult putExtras being recieved as NULL
您好,我正在创建 SideActivity 来收集一些结果并将它们传回 Main Activity。但是,putextra 中的两个字符串值是 NULL,而不是字符串“20”和 jamSize“medium”。有没有办法正确传递数据?
在我的 Main Activity 中,我有一个 setOnClickListener 和一个 onActivityResult 函数。
jamButton.setOnClickListener {
var intent = Intent(this@MainActivity, SideActivity::class.java)
intent.putExtra("jamName", "raspberry")
intent.putExtra("jamPrice", "12.00")
startActivityForResult(intent, 1) // passing request code value 1
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == RESULT_OK) {
val jamPrice:String = intent.getStringExtra("jamPrice").toString()
val jamSize:String = intent.getStringExtra("jamSize").toString()
val newJam = DataModel("Jam", "$jamSize", "$jamPrice")
list.add(0, newJam)
jamAdapter.notifyItemInserted(0)
}
}
这是我的第二个Activity
completeBtn.setOnClickListener {
val jamPrice: String = textView2.text.toString()
val jamSize: String = textView3.text.toString()
val intent = Intent(this@SideActivity, MainActivity::class.java)
intent.putExtra("jamPrice", "20.00")
intent.putExtra("jamSize", jamSize)
setResult(Activity.RESULT_OK, intent)
finish()
}
在你的 onActivityResult 中不要使用这个:
val jamPrice:String = intent.getStringExtra("jamPrice").toString()
因为 intent 变量是 activity 的 Intent 而不是使用 :
val jamPrice: String = data?.getStringExtra("jamPrice").toString()
您好,我正在创建 SideActivity 来收集一些结果并将它们传回 Main Activity。但是,putextra 中的两个字符串值是 NULL,而不是字符串“20”和 jamSize“medium”。有没有办法正确传递数据?
在我的 Main Activity 中,我有一个 setOnClickListener 和一个 onActivityResult 函数。
jamButton.setOnClickListener {
var intent = Intent(this@MainActivity, SideActivity::class.java)
intent.putExtra("jamName", "raspberry")
intent.putExtra("jamPrice", "12.00")
startActivityForResult(intent, 1) // passing request code value 1
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == RESULT_OK) {
val jamPrice:String = intent.getStringExtra("jamPrice").toString()
val jamSize:String = intent.getStringExtra("jamSize").toString()
val newJam = DataModel("Jam", "$jamSize", "$jamPrice")
list.add(0, newJam)
jamAdapter.notifyItemInserted(0)
}
}
这是我的第二个Activity
completeBtn.setOnClickListener {
val jamPrice: String = textView2.text.toString()
val jamSize: String = textView3.text.toString()
val intent = Intent(this@SideActivity, MainActivity::class.java)
intent.putExtra("jamPrice", "20.00")
intent.putExtra("jamSize", jamSize)
setResult(Activity.RESULT_OK, intent)
finish()
}
在你的 onActivityResult 中不要使用这个:
val jamPrice:String = intent.getStringExtra("jamPrice").toString()
因为 intent 变量是 activity 的 Intent 而不是使用 :
val jamPrice: String = data?.getStringExtra("jamPrice").toString()