[英]Attempt to invoke virtual method 'java.lang.String[] android.os.Bundle.getStringArray(java.lang.String)' on a null object reference
[英]Android Error : Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
我正在使用Android Studio開發一個Android應用,但出現此錯誤:
嘗試在空對象引用上調用虛擬方法'java.lang.String android.content.Intent.getStringExtra(java.lang.String)'
所以看來我的意圖是空的,但我不明白為什么...
我的第一堂課:計時器
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
}
}
}
我認為您需要在DisplayMessageActivity類中將此導入導入android.provider.AlarmClock.EXTRA_MESSAGE,以便具有EXTRA_MESSAGE消息值。 您的EXTRA_MESSAGE變量當前不存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.