2

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>

  • So `getStartedBtn` is null. Did you use `findViewById()` to initialize it? – forpas Apr 02 '19 at 21:17
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jack Apr 02 '19 at 21:18
  • 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 – Bruno Diego Martins Apr 02 '19 at 21:19
  • make sure your `getStartedBtn` is properly initialized before you call `setOnClickListener` on it. – Jack Apr 02 '19 at 21:20
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Tamir Abutbul Apr 02 '19 at 21:22
  • Maybe you are not applying `android-kotlin-extensions` plugin in build.gradle – EpicPandaForce Apr 02 '19 at 21:28
  • I checked and I am applying the android-kotlin-extensions plug in. I think it auto filled that when I entered that section of code. Thanks you for the suggestion. I doappreciate it. – jason_proger Apr 02 '19 at 22:14

3 Answers3

1

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" />
Bruno Diego Martins
  • 1,048
  • 1
  • 8
  • 27
  • 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. – jason_proger Apr 02 '19 at 21:46
  • I changed the class MainActivity as suggested and still received the same error. – jason_proger Apr 02 '19 at 21:57
  • In this case, will be better whether you provide more from your code. – Bruno Diego Martins Apr 02 '19 at 22:01
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.

0

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

underoid
  • 369
  • 3
  • 11