Using setOnClickListener and getting "Potential NullPointerException. The resource is missing in some of layout versions

implementing onclicklistener in android
setonclicklistener kotlin
setonclicklistener android studio
setonclicklistener java
if button is clicked android studio
view.onclicklistener not working
how to check which button is clicked in android
error setonclicklistener android

I'm using the following code to use the setOnClickListener and every time I run the program it crashes before it runs. I get "app has stopped".

In the logcat, it gives me this error:

2019-04-02 16:03:26.184 6592-6592/com.example.swoosh E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.swoosh, PID: 6592 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.swoosh/com.example.swoosh.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

If I take out the setOnClickListener section of code the program runs. Here is the section of code that is causing the error. Below that, I'll post the section of XML layout where the toggle button is located.

I'm using Android Studio 3.3.2 What am I missing here?

    getStartedBtn.setOnClickListener {
        val leagueIntent=Intent(this, leagueActivity::class.java)
        startActivity(leagueIntent)
    }

<Button android:text="@string/get_started"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:fontFamily="@font/montserrat"
            app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
            android:id="@+id/getStartedBtn" android:typeface="normal" android:textSize="14sp"
            android:textColor="@color/colorAccent" android:background="@drawable/swoosh_button"
            android:layout_marginBottom="24dp" app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/textView3"
            app:layout_constraintHorizontal_bias="0.0" app:layout_constraintVertical_bias="0.929" />

//this is full welcomeActivity.kt file
package com.example.swoosh
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_welcome.*

class MainActivity : AppCompatActivity() {
   override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_welcome)   
    getStartedBtn.setOnClickListener {
      startActivity(Intent(this, LeagueActivity::class.java))
    }
   }
}
Side note: the "getStartedBtn" is highlighed yellow and when I hold mouse over it, it says "Potential Null Pointer exception.  The resource is missing in some of layout versions"

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.swoosh">

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    <activity android:name=".LeagueActivity">
    </activity>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts"/>
</application>

MainActivity.kt
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        getStartedBtn.setOnClickListener {
            startActivity(Intent(this, LeagueActivity::class.java))
        }
    }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/getStartedBtn"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="24dp"
        android:text="@string/get_started"
        android:textColor="@color/colorAccent"
        android:textSize="14sp"
        android:typeface="normal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintVertical_bias="0.929" />

</android.support.constraint.ConstraintLayout>

Don't forget to declare the LeagueActivity on AndroidManifest

<activity android:name=".LeagueActivity" />

4 Ways To Implement OnClickListener On Android, Here are four ways to implement OnClickListener in Android without the Before I get into the reasons why I'm not a fan let's first take a look at� I get that findViewById will get the button widget and then it'll cast it. Then, it's going to use the button to call the setOnClickListener method. However, I don't know what is that argument being passed into the setOnClickListener and I have never seen code like that before.

I got it working. I changed two things, not sure what one did it but I ran the program and it worked:

  1. I changed my "welcomeActivity.kt" file name to caps "WelcomeActivity.kt" per Bruno Diego Martins suggestion earlier about using caps on the file names.

  2. I noticed one of my layout files had a duplicate. The file had the same name as my activity_welcome.xml except it had a (16) at the end of it. I looked at that file and noticed the toggle button for that layout had a different name ID. Perhaps the setOnClickListener was trying to use that (16) file with the wrong button name instead of the original file that I was working in that had the correct name of getStartedBtn. So I deleted the file that had (16) at the end. I then ran the program and it worked.

Thanks for everyone's help. It was my first post on here and I was taken by surprise (pleasantly surprised) how quickly people offered their help. Great community here.

How to use setOnClickListener method in android.view.View, setText(childArray.get(groupPosition).get(childPosition).title); child. setOnClickListener(new View.OnClickListener() { @Override public void onClick( View v)� Kotlin OnClickListener - SetOnClickListener for Button in Kotlin Android. Kotlin setOnClickListener for Button Android button is a UI element generally used to receive user actions as input. In this tutorial, we shall learn to set OnClickListener for button. A quick look into code What we have done here is, we got the reference to the Button and then used setOnClickListener method to trigger an action when the button is clicked.

Make sure that Button with the same ID exists in both portrait and landscape layouts.

Button, OnClickListener and wires the listener to the button using setOnClickListener( View.OnClickListener) . As a result, the system executes the code you write in� Kotlin setOnClickListener for Button Android button is a UI element generally used to receive user actions as input. In this tutorial, we shall learn to set OnClickListener for button. A quick look into code Quick snippet of code to setOnclickListener for a Button // get reference to button val btn_click_me = findViewById(R.id.btn_ click_me) as Button

View.OnClickListener, onClick. Added in API level 1. public abstract void onClick (View v). Called when a view has been clicked. Parameters. v, View : The view that� We can do this using setOnClickListener() method; setOnClickListener(View.OnClickListener l) This method takes the reference to the Listener and registers a callback to be invoked when the View is clicked. So we have the listener in the same class MainActivity. So we can pass the reference using this. So write the following code inside onCreate().

android:onClick vs setOnClickListener, In this tutorial, we will learn how the android:onClick XML attribute in Android Studio works Duration: 8:49 Posted: 9 May 2019 If the setOnLongClickListener returns false, it means that the setOnClickListener will be triggered. This is known as consuming events. The first case consumes the event. Android Button using Kotlin. We’ll be developing an application that increments the counter of the TextView on a Button click. We will use Kotlin to create the button.

How lambdas work in Kotlin. setOnClickListener transformation , For example, the setOnClickListener function, which in Java is defined as: public void setOnClickListener(OnClickListener Get 2 free ebooks just for attending! Using a Custom ArrayAdapter. When we want to display a series of items into a list using a custom representation of the items, we need to use our own custom XML layout for each item. To do this, we need to create our own custom ArrayAdapter class. See this repo for the source code. First, we often need to define a model to represent the data

Comments
  • So getStartedBtn is null. Did you use findViewById() to initialize it?
  • Possible duplicate of What is a NullPointerException, and how do I fix it?
  • Please, post the whole class, is not possible to know how getStartedBtn is being instantiated. Also, your class leagueActivity should be renamed to LeagueActivity and declared on AndroidManifest
  • make sure your getStartedBtn is properly initialized before you call setOnClickListener on it.
  • Possible duplicate of What is a NullPointerException, and how do I fix it?
  • Hi Bruno, I renamed LeagueActivity with the caps as you suggested. I am still getting the same error. Thank you for that though as I wasn't aware the lower case was an issue. as far as the remaining part of your answer, are you suggesting I create a new .kt file MainActivity with that code you posted? Sorry I hate to say it but I'm fairly new and still learning my way around.
  • I changed the class MainActivity as suggested and still received the same error.
  • In this case, will be better whether you provide more from your code.
  • ok I'll add more code in the edit post now. Really appreciate everyone's help.