cant set onclick on recylerview(cardview)

MainActivity.java

public class MainActivity extends AppCompatActivity {

RecyclerView recyclerView;
DatabaseReference reference;
FirebaseRecyclerAdapter<Bookdeets,Bkhomeholder>adapter;
FirebaseRecyclerOptions<Bookdeets> options;
ProgressBar loading;
FloatingActionButton searchbtn;
TextView logout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    reference = FirebaseDatabase.getInstance().getReference().child("books");
    reference.keepSynced(true);
    recyclerView = (RecyclerView) findViewById(R.id.rv);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
    loading=(ProgressBar)findViewById(R.id.loading);
    searchbtn=(FloatingActionButton)findViewById(R.id.searchbtn);
    logout = (TextView) findViewById(R.id.Logout);
    logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Logout();
        }
    });
    searchbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Opensearchpage();

        }
    });

    options = new FirebaseRecyclerOptions.Builder<Bookdeets>()
            .setQuery(reference, Bookdeets.class).build();
    adapter = new FirebaseRecyclerAdapter<Bookdeets, Bkhomeholder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull Bkhomeholder holder, int position, @NonNull Bookdeets model) {

            Picasso.get().load(model.getImage()).into(holder.bookimg, new Callback() {
                @Override
                public void onSuccess() {
                    loading.setVisibility(View.GONE);

                }

                @Override
                public void onError(Exception e) {
                    Toast.makeText(getApplicationContext(), "could not get the image", Toast.LENGTH_LONG).show();
                    loading.setVisibility(View.GONE);
                }
            });

            holder.title.setText(model.getBookname());

        }

        @NonNull
        @Override
        public Bkhomeholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewlay, parent, false);
            return new Bkhomeholder(view);
        }
            };
    GridLayoutManager gridLayoutManager=new GridLayoutManager(getApplicationContext(),3);
    recyclerView.setLayoutManager(gridLayoutManager);
    adapter.startListening();
    recyclerView.setAdapter(adapter);
}

@Override
protected void onStart() {
    super.onStart();
    if(adapter!=null)
        adapter.startListening();
    loading.setVisibility(View.VISIBLE);
}

@Override
protected void onStop() {
    super.onStop();
    if(adapter!=null)
        adapter.stopListening();
}

@Override
protected void onResume() {
    super.onResume();
    if(adapter!=null)
        adapter.startListening();
}
public void Opensearchpage(){
    Intent intent=new Intent(this, SearchPage.class);
    startActivity(intent);
}
public void Logout() {
    FirebaseAuth.getInstance().signOut();
    startActivity(new Intent(getApplicationContext(), Login.class));
    finish();
}

Bkhomeholder.java

public class Bkhomeholder extends RecyclerView.ViewHolder {
public TextView title;
public ImageView bookimg;

public Bkhomeholder(@NonNull View itemView) {
    super(itemView);
    title = itemView.findViewById(R.id.bkdettitle);

    bookimg = itemView.findViewById(R.id.bkdetimg);

}    

activity_main.xml

<RelativeLayout 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"
tools:context=".homepage.MainActivity"
android:orientation="vertical">


<TextView
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_marginTop="4dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:text="Library App"
    android:textAlignment="center"
    android:textColor="#E21B1B"
    android:textSize="32dp"

    android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
    android:clipChildren="true"
    android:id="@+id/rv"
    android:layout_width="match_parent"
    android:layout_height="525dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="75dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"/>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/searchbtn"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginStart="345dp"
    android:layout_marginTop="480dp"
    android:layout_marginEnd="12dp"
    android:background="#B3E5FC"
    android:clickable="true"

    app:srcCompat="@drawable/customicon" />

<ProgressBar
    android:id="@+id/loading"
    style="?android:attr/progressBarStyle"
    android:layout_width="76dp"
    android:layout_height="76dp"
    android:layout_marginStart="163dp"
    android:layout_marginTop="253dp"
    android:layout_marginEnd="163dp"
    android:layout_marginBottom="253dp"
    android:visibility="invisible"

    />
<TextView
    android:layout_width="match_parent"
    android:layout_height="25dp"
    android:id="@+id/Logout"
    android:layout_centerHorizontal="true"
    android:text="Logout"
    android:textAlignment="center"
    android:textSize="14sp"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="565dp"

cardview xml

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:id="@+id/bookcardview"
android:layout_width="120dp"
android:layout_height="190dp"
android:layout_margin="5dp"
cardview:cardCornerRadius="4dp">

<LinearLayout
    android:id="@+id/bkdetlinear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:ignore="ExtraText"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    android:layout_marginEnd="4dp"
    >


    <ImageView


        android:id="@+id/bkdetimg"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:background="#2d2d2d"
        android:clickable="true"
        android:scaleType="fitXY" />

    <TextView
        android:id="@+id/bkdettitle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Book title"
        android:textColor="#2d2d2d"
        android:textSize="12sp" />


</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>

i tried setting on click on the cardviewholder that is separate xml file which is inflated in my mainactivity oncreate and set the onclick listener under onBindViewHolder but i got a error regarding null object refference as if the activity could not detect the cardview

Answer

You can set OnClickListener on RecyclerView item inside onBindViewHolder like below:

@Override
protected void onBindViewHolder(@NonNull Bkhomeholder holder, int position, @NonNull Bookdeets model) {

    ....

    holder.title.setText(model.getBookname());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Do your operation here
        }
    });

}

Update: Remove android:clickable="true" from cardview layout like below:

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:cardview="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:foreground="?android:attr/selectableItemBackground"
    android:id="@+id/bookcardview"
    android:layout_width="120dp"
    android:layout_height="190dp"
    android:layout_margin="5dp"
    cardview:cardCornerRadius="4dp">

    <LinearLayout
        android:id="@+id/bkdetlinear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:ignore="ExtraText"
        android:foreground="?android:attr/selectableItemBackground"
        android:layout_marginEnd="4dp"
        >


        <ImageView


            android:id="@+id/bkdetimg"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:background="#2d2d2d"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/bkdettitle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Book title"
            android:textColor="#2d2d2d"
            android:textSize="12sp" />


    </LinearLayout>
</androidx.cardview.widget.CardView>