-1

This code was working fine before but now i get null pointer exception on the button

    import kotlinx.android.synthetic.main.activity_splash.*

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_splash)
            button_splash_getting_started.setOnClickListener {
                val intent = Intent(this@SplashActivity, RegisterActivity::class.java)
                intent.putExtra("FROM_SPLASH_ACTIVITY",true)
                startActivity(intent)
            }
            textView_splash_already_have_account.setOnClickListener {
                val loginIntent = Intent(this@SplashActivity, LoginActivity::class.java)
                loginIntent.putExtra("FROM_SPLASH_ACTIVITY",true)
                startActivity(loginIntent)
            }
        }

error java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

and here is logcat

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edubaba.howApp/com.test.ui.SplashActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
            at com.edubaba.howApp.ui.splash.SplashActivity.onCreate(SplashActivity.kt:35)
            at android.app.Activity.performCreate(Activity.java:7009)
            at android.app.Activity.performCreate(Activity.java:7000)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)

here is the xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/activity_splash_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:tag="customsnackbar">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/activity_splash_constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/imageView_splash_background"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/how_background" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <TextView
        android:id="@+id/activity_splash_skip_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="32dp"
        android:padding="20dp"
        android:text="@string/skip"
        android:textColor="@color/white"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ProgressBar
        android:id="@+id/activity_splash_progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:theme="@style/progressBarWhite"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_splash_getting_started"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="16dp"
        android:background="@drawable/background_button_getting_started"
        android:text="@string/get_started"
        android:textColor="#ec975d"
        android:textSize="18sp"
        app:layout_constraintBottom_toTopOf="@+id/textView_splash_already_have_account"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="798dp" />

    <TextView
        android:id="@+id/textView_splash_already_have_account"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="128dp"
        android:text="@string/already_signed"
        android:textColor="@color/white"
        android:translationZ="1dp"
        app:layout_constraintBottom_toBottomOf="@+id/activity_splash_constraintLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Can anyone tell what could have possibly gone wrong?

2 Answers2

1

This id button_splash_getting_started Doesn't exist on your Xml that's why you getting error.

Replace this id button_splash_getting_started with this button_splash_getting_started1.

That's it .

frankenstein
  • 1,057
  • 6
  • 21
  • no thats not the problem..I was just trying to change the id to see if that works...I just forgot to change it back before adding this code here..I'm sorry – Rubyna Thapa Jan 30 '20 at 08:11
  • if your `id` is correct and you have also imported correct screen `import kotlinx.android.synthetic.main.activity_splash.*` then this will works. – frankenstein Jan 30 '20 at 08:14
0

I think I found the problem. In onCreate() method you have:

setContentView(R.layout.activity_splash) 

But your .xml file have id:

android:id="@+id/activity_splash_root_layout"

So, I think you need replace to setContentView() right id="". For example:

setContentView(R.id.activity_splash_root_layout)
Max Shwed
  • 208
  • 2
  • 9