0

I know this question has been asked before, but since my Android Studio example tutorial app is compiled with a 3 years younger API, and the error looks like having a different cause, I figure it's reason enough to ask it anew.

I was closely following the Android SDK Guide Build your first App and I do it all in Kotlin, not Java. I also differ from the suggested compatibility level (I am using Android 7.0 API 24). The app compiles ok (only 1 warning bout NDK is missing a "platforms" directory). Whether I run it on my Galaxy S6 with Android 7.0 API 24 or on the virtual Nexus 5X with API 27, the app crashes when it is launching the Second activity to display the message.

There are two activities: Main activity which has an EditText containing some text field and a button to send the text of it as EXTRA_MESSAGE in an intent to the Second activity which is supposed to display it.

Code for the Main activity:

package com.altran.tutorialapp

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

const val TUTORIAL_EXTRA_MESSAGE = "com.altran.tutorialapp.MESSAGE"

class MainActivity : AppCompatActivity() {

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

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

Code for the second activity:

package com.altran.tutorialapp

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(TUTORIAL_EXTRA_MESSAGE)

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

And here is the excerpt from logcat with the virtual Nexus 5X, which is the same error I get also in a proper bug report on Galaxy S6

03-16 18:14:20.786 3843-3843/com.altran.tutorialapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.altran.tutorialapp, PID: 3843
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.altran.tutorialapp/com.altran.tutorialapp.DisplayMessageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
    at com.altran.tutorialapp.DisplayMessageActivity.<init>(DisplayMessageActivity.kt:16)
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

But I don't know how to prevent this. The stack trace points to the line

val message = intent.getStringExtra(TUTORIAL_EXTRA_MESSAGE)

and Android Studio shows this hint:

Declaration has type inferred from a platform call, which can lead to unchecked nullability issues. Specify type explicitly as nullable or non-nullable. This inspection reports functions and properties that have platform type. In order to prevent unexpected errors, the type should be declared explicitly.

When I rewrite the line as ...

private val message: String = intent.getStringExtra(TUTORIAL_EXTRA_MESSAGE)

... the same error happens. What else can I do? This is my first app and first Kotlin code, so please be gentle.

Zababa
  • 1,268
  • 1
  • 10
  • 20

1 Answers1

3

Try like this. You are calling intent when before activity created. Let me know if it helps. Also take a look at here https://kotlinlang.org/docs/tutorials/android-plugin.html you can use kotlin view binding instead of findViewById.

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(TUTORIAL_EXTRA_MESSAGE)

        // Capture this layout's TextView and set its string to the string of the EXTRA
        val textView = findViewById<TextView>(R.id.textView)
        textView.text = message

    }


}
toffor
  • 883
  • 1
  • 7
  • 18