简体   繁体   English

不一致检测到无效的 View Holder 适配器错误

[英]Inconsistency Detected Invalid View Holder Adapter Error

I made an application running with Firestore for my school project.我为我的学校项目制作了一个使用 Firestore 运行的应用程序。 When I add the first query there is no problem but if I want to add the second one and go back to MainActivity , I get this error and the application dies.当我添加第一个查询时没有问题,但如果我想将第二个查询和 go 添加回MainActivity ,我会收到此错误并且应用程序死机。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.caneraltuner.cepanket2, PID: 13240
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionNoteHolder{f9f3667 position=2 id=-1, oldPos=0, pLpos:0 scrap [attachedScrap] tmpDetached no parent} androidx.recyclerview.widget.RecyclerView{c64d4da VFED..... ........ 0,0-1080,1584 #7f0801de app:id/recycler_view}, adapter:com.caneraltuner.cepanket2.NoteAdapter@2549b6d, layout:androidx.recyclerview.widget.LinearLayoutManager@8af93a2, context:com.caneraltuner.cepanket2.MainActivity@c95bf4d
    at androidx.recyclerview.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:6156)
    at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6339)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
    at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
    at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
    at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
    at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
    at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4255)
    at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4010)
    at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:2028)
    at androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:417)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
    at android.view.Choreographer.doCallbacks(Choreographer.java:796)
    at android.view.Choreographer.doFrame(Choreographer.java:727)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
    I/Process: Sending signal. PID: 13240 SIG: 9

Code in MainActivity: MainActivity中的代码:

package com.caneraltuner.cepanket2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;

public class MainActivity extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference notebookRef = db.collection("Cevaplar");
private NoteAdapter adapter;

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

    FloatingActionButton button = findViewById(R.id.button_add_note);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, NewNoteActivity.class));
        }
    });
    setUpRecyclerView();
}

private void setUpRecyclerView() {
    Query query = notebookRef.orderBy("priority", Query.Direction.DESCENDING);
    FirestoreRecyclerOptions<Note> options = new FirestoreRecyclerOptions.Builder<Note>()
            .setQuery(query, Note.class)
            .build();
    adapter = new NoteAdapter(options);
    RecyclerView recyclerView = findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);

    new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            adapter.deleteItem(viewHolder.getAdapterPosition());
        }
    }).attachToRecyclerView(recyclerView);
}

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();
}
}

Code in NewNoteActivity: NewNoteActivity 中的代码:

package com.caneraltuner.cepanket2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;

public class NewNoteActivity extends AppCompatActivity {

private RadioButton radioButton, radioButton2, radioButton3, radioButton4, radioButton5, radioButton6,
        radioButton7, radioButton8, radioButton9, radioButton10, radioButton11, radioButton12;
String cevap1, cevap2, cevap3, cevap4;

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

    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
    setTitle("Yeni Anket Yap");

    radioButton = findViewById(R.id.radioButton);
    radioButton2 = findViewById(R.id.radioButton2);
    radioButton3 = findViewById(R.id.radioButton3);
    radioButton4 = findViewById(R.id.radioButton4);
    radioButton5 = findViewById(R.id.radioButton5);
    radioButton6 = findViewById(R.id.radioButton6);
    radioButton7 = findViewById(R.id.radioButton7);
    radioButton8 = findViewById(R.id.radioButton8);
    radioButton9 = findViewById(R.id.radioButton9);
    radioButton10 = findViewById(R.id.radioButton10);
    radioButton11 = findViewById(R.id.radioButton11);
    radioButton12 = findViewById(R.id.radioButton12);
}

public void onRadioButtonClicked(View view) {
    //RadioButton tıklama kontrolü
    boolean kontrol = ((RadioButton) view).isChecked();

    // Hangi RadioButton'ın tıklandığının kontrolü
    switch (view.getId()) {
        case R.id.radioButton:
            if (kontrol)
                cevap1 = radioButton.getText().toString();
            break;
        case R.id.radioButton2:
            if (kontrol)
                cevap1 = radioButton2.getText().toString();
            break;
        case R.id.radioButton3:
            if (kontrol)
                cevap1 = radioButton3.getText().toString();
            break;
        case R.id.radioButton4:
            if (kontrol)
                cevap2 = radioButton4.getText().toString();
            break;
        case R.id.radioButton5:
            if (kontrol)
                cevap2 = radioButton5.getText().toString();
            break;
        case R.id.radioButton6:
            if (kontrol)
                cevap2 = radioButton6.getText().toString();
            break;
        case R.id.radioButton7:
            if (kontrol)
                cevap3 = radioButton7.getText().toString();
            break;
        case R.id.radioButton8:
            if (kontrol)
                cevap3 = radioButton8.getText().toString();
            break;
        case R.id.radioButton9:
            if (kontrol)
                cevap3 = radioButton9.getText().toString();
            break;
        case R.id.radioButton10:
            if (kontrol)
                cevap4 = radioButton10.getText().toString();
            break;
        case R.id.radioButton11:
            if(kontrol)
                cevap4 = radioButton11.getText().toString();
        case R.id.radioButton12:
            if(kontrol)
                cevap4 = radioButton12.getText().toString();
            break;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.new_note_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.save_note:
            saveNote();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

private void saveNote() {
    String title = "Anket";
    int priority = 1;
    if (cevap1.equals("") || cevap2.equals("") || cevap3.equals("") || cevap4.equals("")) {
        Toast.makeText(this, "Lütfen tüm cevapları eksiksiz seçin", Toast.LENGTH_SHORT).show();
        return;
    }

    CollectionReference reference = FirebaseFirestore.getInstance().collection("Cevaplar");
    reference.add(new Note(title, cevap1, cevap2, cevap3, cevap4, priority));
    Toast.makeText(this, "Cevaplar kaydedildi", Toast.LENGTH_SHORT).show();
    finish();
}

}

I solved my problem.我解决了我的问题。 The problem happens in MainActivity , it is caused by RecyclerView Data that has been modified in different thread.问题发生在MainActivity ,是由RecyclerView Data 在不同线程中修改引起的。 checking all data access and wrapping LinearLayoutManager would be considered as a solution.检查所有数据访问和包装LinearLayoutManager将被视为解决方案。

I wrote the code gave by @sakiM at first answer: RecyclerView and java.lang.IndexOutOfBoundsException: Inconsistency detected.我在第一个答案中写了@sakiM 给出的代码: RecyclerView and java.lang.IndexOutOfBoundsException: Inconsistency detected。 Invalid view holder adapter positionViewHolder in Samsung devices Here is the code: 三星设备中无效的视图支架适配器 positionViewHolder下面是代码:

public class WrapContentLinearLayoutManager extends LinearLayoutManager {
    public WrapContentLinearLayoutManager(Context context) {
        super(context);
    }

    public WrapContentLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
        super(context, orientation, reverseLayout);
    }

    public WrapContentLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
        try {
            super.onLayoutChildren(recycler, state);
        } catch (IndexOutOfBoundsException e) {
            Log.e("TAG", "meet a IOOBE in RecyclerView");
        }
    }
}
  • Then i matched my RecyclerView to WrapContentLinearLayoutManager然后我将我的RecyclerView匹配到WrapContentLinearLayoutManager

Here is the code:这是代码:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new WrapContentLinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

Thank you all, have a nice day谢谢大家,祝你有美好的一天

I solved it by using RecyclerView inside Relative/LinearLayout inside NestedScrollView inside CoordinatorLayout我通过在 CoordinatorLayout 内的 NestedScrollView 内使用 Relative/LinearLayout 内的 RecyclerView 解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误是在 firebaseRecyclerAdapter 视图适配器部分 - The error is inn the firebaseRecyclerAdapter view adapter section 使用 FirebaseRecyclerAdapter 时如何对视图持有者绑定进行编码 - How to code the view holder binding when using FirebaseRecyclerAdapter 字节数组到字符串转换不一致 - Byte Array To String Conversion Inconsistency 调用signJWT时出现无效参数错误 - Invalid Argument error when calling signJWT 使用 Firebase 匿名身份验证,错误,JSON 无效 - Using Firebase Anonymous Authentication, error, invalid JSON AWS Sagemaker 错误 INVALID_INFERENCE_TYPE - AWS Sagemaker error INVALID_INFERENCE_TYPE Terraform:错误:Kube.netes 集群不可访问:配置无效 - Terraform: Error: Kubernetes cluster unreachable: invalid configuration AWS Lambda 发布 - 错误 NU1605:检测到 package 降级 - AWS Lambda Publish - error NU1605: Detected package downgrade 收到此错误 Firebase Error (auth/invalid-api-key) - Getting this error Firebase Error (auth/invalid-api-key) 使用 Apple ID 登录 Firebase 时出现错误“invalid_request: Invalid web redirect url” - Getting error "invalid_request: Invalid web redirect url" when using Apple ID sign-in with Firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM