пытается установить один флажок за раз в окне просмотра, но приложение вылетает - PullRequest
0 голосов
/ 23 апреля 2020

Я пытаюсь установить один флажок за раз в обзоре переработчика, так как мне нужны данные из элемента, чтобы я мог использовать его в другом методе. В настоящее время я могу выбрать элемент, и данные затем сохраняются в переменной, которая затем используется в другом классе, но если я пытаюсь выбрать другой элемент, чтобы он стал новым и единственным выбранным элементом в представлении реселлера, происходит сбой. Используемая переменная:

 private int lastCheckedPos = -1;

Я использую адаптер для утилизации пожарной базы

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));

            }
        };

Это моя полная трассировка стека

 Process: com.example.carcrashdetection, PID: 16888
    java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling androidx.recyclerview.widget.RecyclerView{f5f26ef VFED..... ......ID 0,1821-1440,2521 #7f08005b app:id/contactsView}, adapter:com.example.carcrashdetection.Home$1@53a95fc, layout:androidx.recyclerview.widget.LinearLayoutManager@8cd485, context:com.example.carcrashdetection.MainActivity@2b025f1
        at androidx.recyclerview.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:3051)
        at androidx.recyclerview.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:5536)
        at androidx.recyclerview.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:12253)
        at androidx.recyclerview.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:7354)
        at com.example.carcrashdetection.Home$1$1.onCheckedChanged(Home.java:118)
        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)
2020-04-23 15:57:53.185 16888-16888/com.example.carcrashdetection E/AndroidRuntime:     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 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)

Адаптер контактов

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>

Я могу видеть из трассировки стека, что это дает мне исключение NullPointerException, но на самом деле не знаю, почему. Любые предложения будут высоко оценены.

1 Ответ

0 голосов
/ 23 апреля 2020

Вам не нужно снова инициировать просмотр, вы можете использовать его holder, чтобы получить его.

Примерно так:

number = ((TextView) holder.itemView.findViewById(R.id.contactPhone)).getText().toString();

Эта проблема возникает из-за жизненного цикла данного элемента. иметь в RecyclerAdapter.

Еще одна вещь, которую я могу вам сказать, это: Внимание с NullPointerException, вы можете использовать проверку != null, чтобы те же проблемы не возникали снова.

TextView tvNumber = holder.itemView.findViewById(R.id.contectPhone);

String number = tvNumber != null ? tvNumber.getText().toString() : ""

Скажите, если Ваши проблемы были решены:)

...