当我去另一项活动并且没有做任何事情并返回到之前的活动时,应用程序崩溃将发生以下给定的错误。我有相同的项目但那是 2 年前构建的并且该项目将正常工作但是当我移动该项目时新项目会出现错误
`E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.loganbeast.pdf, PID: 5560
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionmyviewholder{de30495 position=5 id=-1, oldPos=0, pLpos:0 scrap [attachedScrap] tmpDetached no parent} androidx.recyclerview.widget.RecyclerView{21e5fd2 VFED..... ......I. 0,0-1080,2100 #7f080168 app:id/recview}, adapter:com.loganbeast.pdf.myadapter@37563a8, layout:androidx.recyclerview.widget.LinearLayoutManager@68cf8a3, context:com.loganbeast.pdf.MainActivity@5952061
at androidx.recyclerview.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:6544)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6727)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6688)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6684)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2362)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1662)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1622)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:687)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4550)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4305)
at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4873)
at android.view.View.layout(View.java:23550)
at android.view.ViewGroup.layout(ViewGroup.java:6452)
at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
at android.view.View.layout(View.java:23550)
at android.view.ViewGroup.layout(ViewGroup.java:6452)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23550)
at android.view.ViewGroup.layout(ViewGroup.java:6452)
at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:536)
at android.view.View.layout(View.java:23550)
at android.view.ViewGroup.layout(ViewGroup.java:6452)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:332)
at android.widget.FrameLayout.onLayout(FrameLayout.java:270)
at android.view.View.layout(View.java:23550)
at android.view.ViewGroup.layout(ViewGroup.java:6452)
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:23550)
at android.view.ViewGroup.layout(ViewGroup.java:6452)
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:868)
at android.view.View.layout(View.java:23550)
at android.view.ViewGroup.layout(ViewGroup.java:6452)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3858)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3308)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2272)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8999)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1160)
at android.view.Choreographer.doCallbacks(Choreographer.java:950)
at android.view.Choreographer.doFrame(Choreographer.java:879)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1142)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:210)
at android.os.Looper.loop(Looper.java:299)
at android.app.ActivityThread.main(ActivityThread.java:8319)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038)`
*
主要活动代码** 我认为错误出在这个活动中。如果有人能解决我的错误,请帮助我
`
package com.loganbeast.pdf;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;`
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.database.FirebaseDatabase;
public class MainActivity extends AppCompatActivity
{
FloatingActionButton fb;
RecyclerView recview;
myadapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
fb = (FloatingActionButton) findViewById(R.id.floatingActionButton);
fb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), uploadfile.class));
}
});
recview = (RecyclerView) findViewById(R.id.recview);
recview.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<model> options =
new FirebaseRecyclerOptions.Builder<model>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("mydocuments"), model.class)
.build();
adapter=new myadapter(options);
recview.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
`
适配器类代码 `
`public class myadapter extends FirebaseRecyclerAdapter<model,myadapter.myviewholder>
{
public myadapter(@NonNull FirebaseRecyclerOptions<model> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull final myviewholder holder, int position, @NonNull final model model) {
holder.header.setText(model.getFilename());
holder.img1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(holder.img1.getContext(),viewpdf.class);
intent.putExtra("filename",model.getFilename());
intent.putExtra("fileurl",model.getFileurl());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
holder.img1.getContext().startActivity(intent);
}
});
}
@NonNull
@Override
public myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.singlerowdesign,parent,false);
return new myviewholder(view);
}
public class myviewholder extends RecyclerView.ViewHolder
{
ImageView img1;
TextView header;
public myviewholder(@NonNull View itemView)
{
super(itemView);
img1=itemView.findViewById(R.id.img1);
header=itemView.findViewById(R.id.header);
}
}
}`