jeudi 23 avril 2020

trying to select one checkbox at a time in a recyclerview but app crashes

I am trying to select one checkbox at a time in a recyclerview as i need data from an item so that i can use it in a different method. Currently I can select an item and the data is then stored in a variable which is then used in a different class but if i try and select a different item so that it becomes the new and only selected item in the recyclerview, it crashes. Variable used:

 private int lastCheckedPos = -1;

I am using a firebase recycler adapter

adapter1 = new FirebaseRecyclerAdapter<contact, contactAdapter>(options1) {
            @Override
            protected void onBindViewHolder(@NonNull final contactAdapter holder, final int position, @NonNull contact model) {
                holder.contactName.setText(model.getContactName());
                holder.contactPhone.setText(model.getContactPhone());
                holder.chk.setChecked(position == lastCheckedPos);
                holder.chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (position == lastCheckedPos) {
                            holder.chk.setChecked(false);
                            lastCheckedPos = -1;
                        }else {
                            lastCheckedPos = position;
                            for (int i = 0; i < adapter1.getItemCount(); i++) {
                                if (adapter1.getItem(i).isSelected(true)) {
                                    number = ((TextView) contactsView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.contactPhone)).getText().toString();
                                    Toast.makeText(getActivity(), "Data Inserted....." + number, Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "Error", Toast.LENGTH_LONG).show();
                                }
                            }
                            notifyDataSetChanged();
                        }
                    }
                });
            }

            @NonNull
            @Override
            public contactAdapter onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new contactAdapter(LayoutInflater.from(getActivity()).inflate(R.layout.item_contacts, parent, false));

            }
        };

This is my full stacktrace

2020-04-23 14:48:45.528 10913-10913/com.example.carcrashdetection E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.carcrashdetection, PID: 10913
    java.lang.NullPointerException: Attempt to read from field 'android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView' on a null object reference
        at com.example.carcrashdetection.Home$1$1.onCheckedChanged(Home.java:112)
        at android.widget.CompoundButton.setChecked(CompoundButton.java:180)
        at com.example.carcrashdetection.Home$1.onBindViewHolder(Home.java:101)
        at com.example.carcrashdetection.Home$1.onBindViewHolder(Home.java:96)
        at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
        at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
        at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1829)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1673)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1582)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
2020-04-23 14:48:45.528 10913-10913/com.example.carcrashdetection E/AndroidRuntime:     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:780)
        at android.view.View.layout(View.java:22085)
        at android.view.ViewGroup.layout(ViewGroup.java:6290)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3332)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2809)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1929)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7987)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1154)
        at android.view.Choreographer.doCallbacks(Choreographer.java:977)
        at android.view.Choreographer.doFrame(Choreographer.java:893)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1139)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

Contacts Adapter

public class contactAdapter extends RecyclerView.ViewHolder{

    public TextView contactName, contactPhone;
    CheckBox chk;
    AdapterView.OnItemClickListener itemClickListener;



    public contactAdapter(@NonNull View itemView) {
        super(itemView);
        contactName = itemView.findViewById(R.id.contactName);
        contactPhone = itemView.findViewById(R.id.contactPhone);
        chk = itemView.findViewById(R.id.myCheckBox);
    }
}

item_contacts.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/outer"
        android:layout_width="match_parent"
        android:layout_height="101dp"
        android:layout_marginBottom="24dp"
        android:paddingBottom="24dp"
        android:paddingTop="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="65dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/icon" />

        <LinearLayout
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="vertical"
            android:id="@+id/inner">

            <TextView
                android:id="@+id/contactName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Name"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/contactPhone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Phone"
                android:textSize="16sp" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/hello"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal"
            android:padding="10dp">

            <Button
                android:id="@+id/edit"
                android:layout_width="70dp"
                android:layout_height="wrap_content"


                android:text="Edit"
                android:textSize="14sp" />

            <Button
                android:id="@+id/delete"
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="Delete"
                android:textSize="14sp" />
        </LinearLayout>


        <CheckBox
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:height="60dp"
            android:id="@+id/myCheckBox"
            />

    </LinearLayout>

</LinearLayout>

I can see from the stack trace that its giving me a NullPointerException but dont really know why. Any suggestions would be much appreciated.




Aucun commentaire:

Enregistrer un commentaire