Sunday 11 June 2017

Android Error : Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference

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(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(R.id.textView).apply {
text = message
}
}
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...