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

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>



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

You must that Button(other views) with the same id exists in both portrait and landscape layouts.

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jason_proger
Solution 2
Solution 3 Halil Ozel