【问题标题】:Receiving null from getParcelableExtra()从 getParcelableExtra() 接收 null
【发布时间】:2019-05-21 13:28:28
【问题描述】:

我有一个类类别,我传递给额外的,点击的类别。 这在 6 个屏幕中有效,但在最后一个屏幕中我收到 null

我把额外的活动放在哪里

adapter.setOnItemClickListener { item, view ->

                    val categories = item as Categories
                    val intent = Intent(view.context, LearningFirstLibras::class.java)
                    intent.putExtra(CATEGORY_KEY, categories.category)
                    startActivity(intent)
                }

类别

@Parcelize
class Category(
               val name: String,
               val imageURL: String,
               val real_name: String,
               val object_1_screen_libras_image: String,
               val object_1_screen_libras_text: String,
               val object_2_screen_libras_text: String,
               val object_2_screen_libras_image: String,
               val object_3_screen_libras_text: String,
               val object_3_screen_libras_image: String,
               val object_1_screen_asl_text: String,
               val object_1_screen_asl_image: String,
               val object_2_screen_asl_text: String,
               val object_2_screen_asl_image: String,
               val object_3_screen_asl_text: String,
               val object_3_screen_asl_image: String,
               val question: String,
               val answer: String,
               val alternative_1: String,
               val alternative_2: String,
               val alternative_3: String,
               val alternative_4: String
                ) : Parcelable {
    constructor() : this("", "", "", "", "",
            "","","","",
            "","","","",
            "","","","","","",
            "","")
}

我得到 null 的活动

class Quiz : AppCompatActivity() {

    var categoryF:Category = Category()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_quiz)

        categoryF = intent.getParcelableExtra(NewWordsActivity.CATEGORY_KEY)

【问题讨论】:

  • 无需创建 constructor() : this("", "", "", "", "", "","","","", "","" ,"","", "","","","","","", "","")
  • 确保 categories.category 不为 null ,尝试调试您的代码。
  • 您正在启动LearningFirstLibras 活动但读取Quiz 活动中的值?
  • 我使用日志来获取 categories.category 和 categories.category 中的 2 个值我得到了值:2019-05-21 09:37:33.436 5433-5433/com.educator.dualsignal D /teste: com.educator.dualsignal.models.Category@a870587 / 2019-05-21 09:38:43.790 5622-5622/com.educator.dualsignal D/teste: foods_1 / 2019-05-21 09:38:43.792 5622-5622/com.eductor.dualsignal D/teste:alternativa_1
  • 你应该开始 Quiz 而不是 LearningFirstLibras,可能。

标签: android android-intent kotlin


【解决方案1】:

试试看

使用 Bundle 发送 Parcelable

val intent = Intent(this, ProfilePage::class.java)
var bundle = Bundle()
bundle.putParcelable(CATEGORY_KEY, categories.category)
intent.putExtra("myBundle",bundle)
startActivity(intent)

恢复 Parcelable

val bundle = intent.getBundleExtra("myBundle")
var categoryF = bundle.getParcelable<Category>(NewWordsActivity.CATEGORY_KEY) as Category

【讨论】:

  • 这个答案背后的动机是什么?它应该如何解决问题?将它包装到另一个包中与.putExtra() 的做法有何不同?请详细说明一下好吗?
  • Motive is Vinicius Barros 想要将数据从一个解析到另一个,所以我在我的代码中使用了使用 bundle 传递可打包数据,所以我分享了它。可能有帮助...
  • 我明白这一点,但我的意思是您没有解释为什么您提供的解决方案会/应该起作用。在我看来,添加另一层(由代码中的Bundle 提供)并不能解决问题。
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 1970-01-01
相关资源
最近更新 更多