我正在使用Android Studio开发一个Android应用程序,但我收到了这个错误:
试图在空对象引用上调用虚方法的java.lang.String android.content.Intent.getStringExtra(java.lang.String)‘
所以看起来我的意图是无效的,但是我不明白为什么.
我的第一个类: Timer
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
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
}
}
}
发布于 2018-06-04 06:21:50
我认为您需要在DisplayMessageActivity类中将此导入设置为导入android.provider.AlarmClock.EXTRA_MESSAGE,以便获得EXTRA_MESSAGE消息值。您的EXTRA_MESSAGE变量当前不存在。
https://stackoverflow.com/questions/50671182
复制相似问题