I'm working on a Android app with Android Studio, but I'm getting this error :
Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
So it's seem that my intent is null, but I'm not getting why...
My first class : 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)
}
}
My second class : 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
}
}
}
i think you need to make this import import android.provider.AlarmClock.EXTRA_MESSAGE in your DisplayMessageActivity class so as to have the EXTRA_MESSAGE message value. your EXTRA_MESSAGE variable currently does not exists.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.