busooCombo
busooCombo

Reputation: 51

How to put an onClick on a CardView in an AlertDialog

I'm trying to do a StartActivity of a CardView in an AlertDialog but it does not work

I couldn't find anything similar on the internet, I also tried with a Button, I got the same error (see LogCat at the bottom of the post)

Here is my layout of my AlertDialog:

<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="15dp"
    android:layout_marginEnd="15dp"
    app:cardBackgroundColor="@color/main_color_bg"
    app:cardCornerRadius="4.5dp"
    app:cardElevation="12dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.cardview.widget.CardView
            android:id="@+id/settings_support_btn"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:layout_marginTop="15dp"
            app:cardBackgroundColor="@color/content_background"
            app:cardCornerRadius="4.5dp"
            app:cardElevation="12dp">
        </androidx.cardview.widget.CardView>
        
    </LinearLayout>

</androidx.cardview.widget.CardView>

Here my Main

AlertDialog settingsDialog;

protected void onCreate(Bundle savedInstanceState) {
//Other code here...

  AlertDialog.Builder builder = new AlertDialog.Builder(this);

  View view = getLayoutInflater().inflate(R.layout.settings_dialog, null);
        CardView settingsSupportBtn = (CardView) findViewById(R.id.settings_support_btn);
        settingsSupportBtn.setOnClickListener(view1 -> {
            startActivity(new Intent(Main.this, SettingsSupport.class));
            settingsDialog.dismiss();
        });

        builder.setView(view);
        settingsDialog = builder.create();

        clickEvent();
}

public void clickEvent() {
  settingsBtn.setOnClickListener(view -> {
        settingsDialog.show();
 });

}

Here is my LogCat:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package.test.android/com.package.test.android.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.cardview.widget.CardView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
...
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.cardview.widget.CardView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Upvotes: 1

Views: 177

Answers (1)

A Sarshar
A Sarshar

Reputation: 294

Change this line

CardView settingsSupportBtn = (CardView) findViewById(R.id.settings_support_btn);

to

CardView settingsSupportBtn = (CardView) view.findViewById(R.id.settings_support_btn);

Upvotes: 1

Related Questions