lekster
lekster

Reputation: 1

Second button not working

I have 2 buttons, the first one works fine but not the second one(button5). It crashes the app upon clicking.

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

I defined the buttons as such in the XML file:

<Button
    android:id="@+id/button"
    android:layout_width="182dp"
    android:layout_height="49dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="135dp"
    android:background="@drawable/pay"
    android:fontFamily="@font/roboto"
    android:text="Login"/>

<Button
    android:id="@+id/button5"
    style="?android:attr/borderlessButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="-136dp"
    android:fontFamily="@font/roboto"
    android:text="@string/create_new_account"
    android:textSize="12sp" />

and called it:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.button);
    Button button5 = (Button) findViewById(R.id.button5);
}

public void onClick(View v) {
    if (v.getId() == R.id.button) {
        Intent intent = new Intent(MainActivity.this, detailspage.class);
        startActivity(intent);
    } else if (v.getId() == R.id.button5) {
        Intent intent = new Intent(MainActivity.this, Registration.class);
        startActivity(intent);
    }
}

My manifest file is:

<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=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".detailspage"/>
    <activity android:name=".Registration"/>
    <activity android:name=".confirmBicycle"></activity>
</application>

I have went through many similar questions and tried different ways of writing the code but to no avail. Thanks for any advice!

Upvotes: 0

Views: 57

Answers (3)

Android1005
Android1005

Reputation: 155

I copied your layout and pasted in my layout. Also, I created an activity wherein again I've pasted your code. In my case, it is working fine:

public class Activity1 extends AppCompatActivity implements View.OnClickListener {

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

    Button button = (Button) findViewById(R.id.button);
    Button button5 = (Button) findViewById(R.id.button5);

    // this is the only part which I could not find in your code. 

    button.setOnClickListener(this);
    button5.setOnClickListener(this);
}

@Override
public void onClick(View view) {

    if(view.getId() == R.id.button){
        Intent intent = new Intent(Activity1.this, SplashActivity.class);
        startActivity(intent);
    }

    else if(view.getId() == R.id.button5){
        Intent intent = new Intent(Activity1.this, ActivityLifeCycle.class);
        startActivity(intent);
    }
}

}

Upvotes: 0

Red M
Red M

Reputation: 2789

button.setOnClickListener(this);
button5.setOnClickListener(this);

You are implementing the interface in your activity but not passing your activity instance to the listener.

Upvotes: 1

BilalMr
BilalMr

Reputation: 335

i suggest you to try:

Button button5 = (Button) findViewById(R.id.button5);
     button5.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                  // your code
                }
              });

Upvotes: 0

Related Questions