首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android错误:尝试在空对象引用上调用虚拟方法的java.lang.String android.content.Intent.getStringExtra(java.lang.String)‘

Android错误:尝试在空对象引用上调用虚拟方法的java.lang.String android.content.Intent.getStringExtra(java.lang.String)‘
EN

Stack Overflow用户
提问于 2018-06-04 05:33:57
回答 1查看 1.7K关注 0票数 1

我正在使用Android Studio开发一个Android应用程序,但我收到了这个错误:

试图在空对象引用上调用虚方法的java.lang.String android.content.Intent.getStringExtra(java.lang.String)‘

所以看起来我的意图是无效的,但是我不明白为什么.

我的第一个类: Timer

代码语言:javascript
复制
package com.example.work.lustucru

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.provider.AlarmClock.EXTRA_MESSAGE



const val EXTRA_MESSAGE = "com.example.lustucru.MESSAGE"

class Timer : AppCompatActivity() {

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

    /** Called when the user taps the Send button */
    fun sendMessage(view: View) {
        val editText = findViewById<EditText>(R.id.editText)
        val message = editText.text.toString()
        val intent = Intent(this, DisplayMessageActivity::class.java).apply {
            this.putExtra(EXTRA_MESSAGE, message)
        }
        startActivity(intent)
    }
}

我的第二个类: DisplayMessageActivity

代码语言:javascript
复制
package com.example.work.lustucru

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class DisplayMessageActivity : AppCompatActivity() {

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

        // Get the Intent that started this activity and extract the string
        val message = intent.getStringExtra(EXTRA_MESSAGE)

        // Capture the layout's TextView and set the string as its text
        val textView = findViewById<TextView>(R.id.textView).apply {
            text = message
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-04 06:21:50

我认为您需要在DisplayMessageActivity类中将此导入设置为导入android.provider.AlarmClock.EXTRA_MESSAGE,以便获得EXTRA_MESSAGE消息值。您的EXTRA_MESSAGE变量当前不存在。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50671182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档