Getting Indexoutofboundsexception Only When Resume Activity
My problem is that my App crashes when the MainActivity gets resumed. The error is an IndexOutOfBoundsException, unfortunately without a specific line. And the error says Index: 0,
Solution 1:
Check below code. Hope it will be helpful for you. You need to replace all firebase
firestore
logic to onResume()
method.
publicclassMainActivityextendsAppCompatActivityimplementsRecyclerItemTouchHelper.RecyclerItemTouchHelperListener, NoteListAdapter.NotesAdapterListener {
private FirebaseAuth mFirebaseAuth;
public FirebaseUser mFirebaseUser;
private FirebaseFirestore db;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager recyclerViewLayoutManager;
public NoteListAdapter adapter;
private List<Note> notesList = newArrayList<>();
private ListenerRegistration firestoreListener;
private RecyclerItemTouchHelper.RecyclerItemTouchHelperListener listener;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rvNoteList);
}
@OverridepublicvoidonStart() {
super.onStart();
if (adapter != null) {
adapter.startListening();
}
}
@OverridepublicvoidonStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
@OverrideprotectedvoidonResume() {
super.onResume();
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getInstance().getCurrentUser();
db = FirebaseFirestore.getInstance();
if (adapter != null) {
adapter.startListening();
}
FirebaseFirestoreSettingssettings=newFirebaseFirestoreSettings.Builder().setPersistenceEnabled(true).build();
db.setFirestoreSettings(settings);
Queryquery= db.collection("users").document(firebase_user_uid).collection("notes");
FirestoreRecyclerOptions<Note> response = newFirestoreRecyclerOptions.Builder<Note>().setQuery(query, Note.class).build();
adapter = newNoteListAdapter(notesList, response, MainActivity.this, MainActivity.this);
recyclerViewLayoutManager = newLinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(recyclerViewLayoutManager);
recyclerView.setItemAnimator(newDefaultItemAnimator());
recyclerView.addItemDecoration(newCustomRecyclerViewDivider(this, LinearLayoutManager.VERTICAL, 16));
recyclerView.setAdapter(adapter);
ItemTouchHelper.SimpleCallbackitemTouchHelperCallback=newRecyclerItemTouchHelper(0, ItemTouchHelper.LEFT, MainActivity.this);
newItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(recyclerView);
firestoreListener = db.collection("users").document(firebase_user_uid).collection("notes")
.addSnapshotListener(newEventListener<QuerySnapshot>() {
@OverridepublicvoidonEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.e("LOL", "Listen failed!", e);
return;
}
notesList.clear();
for (DocumentSnapshot doc : documentSnapshots) {
Notenote= doc.toObject(Note.class);
note.setId(doc.getId());
notesList.add(note);
}
}
});
recyclerView.addOnItemTouchListener(newRecyclerViewTouchListener(getApplicationContext(), recyclerView, newRecyclerViewTouchListener.ClickListener() {
@OverridepublicvoidonClick(View view, int position) {
if (notesList != null || notesList.size() <= 0) {
Notenote= notesList.get(position);
updateNote(note);
}
}
@OverridepublicvoidonLongClick(View view, int position) {
}
}))
}}
Solution 2:
just change your code to :
notesList.clear();
for (DocumentSnapshot doc : documentSnapshots) {
Note note = doc.toObject(Note.class);
note.setId(doc.getId());
notesList.add(note);
instead use,
firestoreListener = db.collection("users").document(firebase_user_uid).collection("notes")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
publicvoidonEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (e != null) {
Log.e("LOL", "Listen failed!", e);
notesList=new ArrayList();
for (DocumentSnapshot doc : documentSnapshots) {
Note note = doc.toObject(Note.class);
note.setId(doc.getId());
notesList.add(note);
}
}
}
});
Post a Comment for "Getting Indexoutofboundsexception Only When Resume Activity"