这是我第一次学习在Android中使用Firebase。一切都很好,直到突然间我开始崩溃,我只是不知道为什么会这样。这是我的日志
06-08 19:39:55.064 28217-28217/k.cook.com E/AndroidRuntime: FATAL EXCEPTION:
main
Process: k.cook.com, PID: 28217
java.lang.IllegalArgumentException: Key not found
at com.firebase.ui.database.FirebaseArray.getIndexForKey(FirebaseArray.java:125)
at
com.firebase.ui.database.FirebaseArray.onChildMoved(FirebaseArray.java:97)
at com.google.android.gms.internal.zzegg.zza(Unknown Source:43)
at com.google.android.gms.internal.zzelk.zza(Unknown Source:2)
at com.google.android.gms.internal.zzelq.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
每当我尝试在活动中的两个片段之间切换时,就会发生此错误
这是代码。它发生在firebaseArray类
中public class FirebaseArray<T> extends ObservableSnapshotArray<T>
implements ChildEventListener, ValueEventListener {
private final Query mQuery;
private final List<DataSnapshot> mSnapshots = new ArrayList<>();
/**
* Create a new FirebaseArray with a custom {@link SnapshotParser}.
*
* @param query The Firebase location to watch for data changes. Can also be a slice of a
* location, using some combination of {@code limit()}, {@code startAt()}, and
* {@code endAt()}.
* @see ObservableSnapshotArray#ObservableSnapshotArray(SnapshotParser)
*/
public FirebaseArray(@NonNull Query query, @NonNull SnapshotParser<T> parser) {
super(parser);
mQuery = query;
}
@Override
protected void onCreate() {
super.onCreate();
mQuery.addChildEventListener(this);
mQuery.addValueEventListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
mQuery.removeEventListener((ValueEventListener) this);
mQuery.removeEventListener((ChildEventListener) this);
}
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
int index = 0;
if (previousChildKey != null) {
index = getIndexForKey(previousChildKey) + 1;
}
mSnapshots.add(index, snapshot);
notifyOnChildChanged(ChangeEventType.ADDED, snapshot, index, -1);
}
@Override
public void onChildChanged(DataSnapshot snapshot, String previousChildKey) {
int index = getIndexForKey(snapshot.getKey());
mSnapshots.set(index, snapshot);
notifyOnChildChanged(ChangeEventType.CHANGED, snapshot, index, -1);
}
@Override
public void onChildRemoved(DataSnapshot snapshot) {
int index = getIndexForKey(snapshot.getKey());
mSnapshots.remove(index);
notifyOnChildChanged(ChangeEventType.REMOVED, snapshot, index, -1);
}
@Override
public void onChildMoved(DataSnapshot snapshot, String previousChildKey) {
int oldIndex = getIndexForKey(snapshot.getKey());
mSnapshots.remove(oldIndex);
int newIndex = previousChildKey == null ? 0 : getIndexForKey(previousChildKey) + 1;
mSnapshots.add(newIndex, snapshot);
notifyOnChildChanged(ChangeEventType.MOVED, snapshot, newIndex, oldIndex);
}
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
notifyOnDataChanged();
}
@Override
public void onCancelled(DatabaseError error) {
notifyOnError(error);
}
//the error is thrown here
private int getIndexForKey(String key) {
int index = 0;
for (DataSnapshot snapshot : mSnapshots) {
if (snapshot.getKey().equals(key)) {
return index;
} else {
index++;
}
}
throw new IllegalArgumentException("Key not found");
}
@NonNull
@Override
protected List<DataSnapshot> getSnapshots() {
return mSnapshots;
}
}