0

The app is basically (as of yet) a login/sign up page I'm having trouble switching activities... the app crashes when I add the setOnClickListener.

class LoginActivity extends AppCompatActivity {


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


        configureBtnSignUp();
        configureBtnResetPassword();
    }


    private void configureBtnSignUp(){
        Button btnSignUp = (Button) findViewById(R.id.btn_signup);
        btnSignUp.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, SignUpActivity.class));
            }
        });
    }

    private void configureBtnResetPassword(){
        Button btnResetPassword = (Button) findViewById(R.id.btn_reset_password);
        btnResetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(LoginActivity.this, PwRecoverActivity.class));
            }
        });

    }


}

This is the error code

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 kardacorporation.bandme.LoginActivity.configureBtnSignUp(LoginActivity.java:24)
                      at kardacorporation.bandme.LoginActivity.onCreate(LoginActivity.java:18)
jusimen
  • 297
  • 3
  • 10

2 Answers2

2

The reason is that you are trying to set a listener to a button that don't have reference in your layout.

The problem is related with btn_reset_password

Be sure that in your layout have a button with id btn_reset_password

In your activity_login:

 <Button
     android:id="@+id/btn_reset_password"
     ...
 </Button>

Nice coding!

Faustino Gagneten
  • 2,564
  • 2
  • 28
  • 54
1

According to Logcat, Your problem is

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

It happened in

kardacorporation.bandme.LoginActivity. configureBtnSignUp(LoginActivity.java:24)`

So we should check configureBtnSignUp method first .

Make sure you have btn_signup id in your xml code .

<Button
    android:id="@+id/btn_signup" 
    ...                         />

And you call configureBtnSignUp method before calling configureBtnResetPassword method .

So you should check configureBtnResetPassword method .

And make sure you have btn_reset_password id in your xml code .

<Button
    android:id="@+id/btn_reset_password" 
    ...                         />
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42