【问题标题】:getStringExtra() function of intent is returning nullintent 的 getStringExtra() 函数返回 null
【发布时间】:2023-01-30 02:13:09
【问题描述】:

我有一个编辑文本,我从那里获取文本并将其发送到意图使用putExtra() 但是当getStringExtra() 返回 null 时。

            val intent = Intent(this, MainActivity::class.java)
            if(editWordView.text.isNotEmpty()){
                val word = editWordView.text.toString()
                intent.putExtra("Word", word)
                startActivity(intent)
            }

在第一个日志中,它显示为空,最后一行未执行,因为单词为空。

        val intent = Intent()
        val word = intent.getStringExtra("Word")
        Log.d(TAG, "MainActivity: $word")
        word?.let {
            viewModel.insert(Word(word))
            Log.d(TAG, "onCreate: Inserted $word")
        }

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    删除:

    val intent = Intent()
    

    您正在创建一个新的空 Intent。那将不包含任何额外内容。相反,您需要从活动中获取 Intent:

    • 通过getIntent()用于创建活动的Intent
    • 在onNewIntent()中用于任何Intent用于将此活动的已运行实例带回前台

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      相关资源
      最近更新 更多