Home > other >  KotlinNullPointerException for android::onClick
KotlinNullPointerException for android::onClick

Time:05-12

I`m just started learning Kotlin, and i have a problem with one of the buttons.

In MainActivity i have a button, that shows a popup with EditText and Button. In that popup user enters a city, and variable CITY changes according to user input after pressing a button.

In function cityChange im trying to replace my variable CITY with a text that user enters in EditText named editText. But when user taps on button, my program crashes with a NullPointerException. Can someone explain, or show how to fix that problem?

Exception log

FATAL EXCEPTION: main
    Process: com.example.weatherapp, PID: 16567
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:414)
        at android.view.View.performClick(View.java:7192)
        at android.view.View.performClickInternal(View.java:7166)
        at android.view.View.access$3500(View.java:824)
        at android.view.View$PerformClick.run(View.java:27592)
        at android.os.Handler.handleCallback(Handler.java:888)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:213)
        at android.app.ActivityThread.main(ActivityThread.java:8178)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
        at android.view.View.performClick(View.java:7192) 
        at android.view.View.performClickInternal(View.java:7166) 
        at android.view.View.access$3500(View.java:824) 
        at android.view.View$PerformClick.run(View.java:27592) 
        at android.os.Handler.handleCallback(Handler.java:888) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:213) 
        at android.app.ActivityThread.main(ActivityThread.java:8178) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 
     Caused by: kotlin.KotlinNullPointerException
        at com.example.weatherapp.MainActivity.cityChange(MainActivity.kt:48)
        at java.lang.reflect.Method.invoke(Native Method) 
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409) 
        at android.view.View.performClick(View.java:7192) 
        at android.view.View.performClickInternal(View.java:7166) 
        at android.view.View.access$3500(View.java:824) 
        at android.view.View$PerformClick.run(View.java:27592) 
        at android.os.Handler.handleCallback(Handler.java:888) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:213) 
        at android.app.ActivityThread.main(ActivityThread.java:8178) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101) 
I/Process: Sending signal. PID: 16567 SIG: 9

MainActivity.kt

class MainActivity : AppCompatActivity() {

    var CITY: String = "kyiv, ua"
    val API: String = "7c45088f48678b739d4d8409a2d9d03e"
    private var btn: Button? = null




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

        val menuActivity = findViewById<Button>(R.id.activityButton)
        menuActivity.setOnClickListener(){
            val Intent = Intent(this, MenuActivity::class.java)
            startActivity(Intent)
        }

        var dialog = CustomDialogFragment()
        val clkBtn = findViewById<Button>(R.id.cityChange)

        clkBtn.setOnClickListener {
            dialog.show(supportFragmentManager, "customDialog")
        }

    }

    fun cityChange(view: View){
        btn = findViewById(R.id.changingButton)

        btn!!.setOnClickListener{
            CITY = editText.text.toString()
        }
    }

change_city.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="350dp"
    android:layout_height="200dp"
    android:background="@color/colorPrimaryDark"
    android:padding="5dp"
    android:layout_gravity="center">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="City Change"
            android:textStyle="bold"
            android:textColor="@color/white"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="horizontal">

        <EditText
            android:id="@ id/editText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:gravity="center"
            android:inputType="textPersonName"
            android:text="" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="225dp"
        android:orientation="vertical">

        <Button
            android:onClick="cityChange"
            android:id="@ id/changingButton"
            android:layout_width="match_parent"
            android:layout_height="62dp"
            android:layout_gravity="bottom"
            android:background="@color/bluelight"
            android:text="Submit" />
    </LinearLayout>

</LinearLayout>

CodePudding user response:

Just simply do this

// Variable declaration 
 private var btn: Button? = null

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

        val menuActivity = findViewById<Button>(R.id.activityButton)
        menuActivity.setOnClickListener(){
            val Intent = Intent(this, MenuActivity::class.java)
            startActivity(Intent)
        }

        var dialog = CustomDialogFragment()
        btn= findViewById<Button>(R.id.changingButton)

        clkBtn.setOnClickListener {
            dialog.show(supportFragmentManager, "customDialog")
        }

    }

Or Else You have to Change

This

  val clkBtn = findViewById<Button>(R.id.cityChange)

To

 val clkBtn = findViewById<Button>(R.id.changingButton)

Hope this will helpful to you.

CodePudding user response:

wrong ID

from this

 val clkBtn = findViewById<Button>(R.id.cityChange)

to

 val clkBtn = findViewById<Button>(R.id.changingButton)

in your XML the button id is "changingButton" not "cityChange"

and don't use onClick and setOnClickListener together, just choose one

  • Related