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>