0

Hello Stackoverflow Members, I am trying to create an Intent when a button is pressed to change my activity in my Android App which I develop in Java. However I just get a NullPointerException:

Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at .onCreate(MainActivity.java:32) - Line 32 is the Line with  "button_get_started.setOnClickListener(new View.OnClickListener(){"

Does somebody know how to fix this issue or how to bypass it? Thanks for your help :)

Main Code:

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

//Splash Activity
public class MainActivity extends AppCompatActivity {

    //Attributes
    ImageView imageView_thisfit;
    ImageView imageView_logo;
    public Button button_get_started;




    //Methods

    //This method is executed while creating an instance of the Activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        imageView_thisfit = findViewById(R.id.imageView_thisfit);
        imageView_logo = findViewById (R.id.imageView_logo);
        button_get_started = findViewById(R.id.button_get_started);
        button_get_started.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openRegistrationActivity();
            }
        });

        setContentView(R.layout.activity_splash);
    } //onCreate



    public void openRegistrationActivity(){
        Intent intent = new Intent(MainActivity.this, RegistrationActivity.class);
        startActivity(intent);
    }

And here is my 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    tools:context=".MainActivity">

    <!-- thisFIT -->
    <ImageView
        android:id="@+id/imageView_thisfit"
        android:layout_width="205dp"
        android:layout_height="101dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.18"
        app:srcCompat="@drawable/thisfit" />

    <!-- Logo -->
    <ImageView
        android:id="@+id/imageView_logo"
        android:layout_width="218dp"
        android:layout_height="246dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView_thisfit"
        app:srcCompat="@drawable/logo" />

    <!-- GET STARTED -->
    <Button
        android:id="@+id/button_get_started"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/bg"
        android:text="GET STARTED"
        android:textSize="20sp"
        android:textColor="#FFFFFFFF"
        android:fontFamily="@font/dosis"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.495"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView_logo"
        app:layout_constraintVertical_bias="0.23000002" />

</androidx.constraintlayout.widget.ConstraintLayout>
PLASMA chicken
  • 2,777
  • 2
  • 15
  • 25
d1sturrb
  • 39
  • 1
  • 8
  • 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) – GBlodgett Mar 19 '19 at 19:35
  • nope, not a duplicate! – d1sturrb Mar 19 '19 at 19:37
  • If you believe it is not a duplicate, then [edit] your post to explain how the linked Q/A did not solve your problem – GBlodgett Mar 19 '19 at 19:38

2 Answers2

1

Just move this line

setContentView(R.layout.activity_splash);

at the starting of the onCreate() method.

Like this:-

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    imageView_thisfit = findViewById(R.id.imageView_thisfit);
    imageView_logo = findViewById (R.id.imageView_logo);
    button_get_started = findViewById(R.id.button_get_started);
    button_get_started.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openRegistrationActivity();
        }
    });
}
Nilesh Rathore
  • 886
  • 1
  • 12
  • 22
-1

Move the line setContentView(R.layout.activity_splash); below super.onCreate(savedInstanceState);

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        imageView_thisfit = findViewById(R.id.imageView_thisfit);
        imageView_logo = findViewById (R.id.imageView_logo);
        button_get_started = findViewById(R.id.button_get_started);
        button_get_started.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openRegistrationActivity();
            }
        });


    }
Ravi
  • 881
  • 1
  • 9
  • 23