我想讀取Firebase實時數據庫中的所有元素(通過導入.json文件(未使用我的應用程序填充)填充所有元素。因此,我嘗試了很多其他解決方案但他們沒有工作,這是我的實時數據庫:從Firebase實時數據庫中讀取應用程序時崩潰
這裏是我閱讀的主要活動的數據:
public final ArrayList<Parking> parkingArrayList=new ArrayList<Parking>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
firebaseDatabase.setPersistenceEnabled(true);
DatabaseReference databaseReference=firebaseDatabase.getReference();
databaseReference.child("Parking").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
Parking p=dataSnapshot1.getValue(Parking.class);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
這裏是我的停車類:
public class Parking implements Comparable<Parking>,Parcelable {
public String name;
public String zone;
public String location;
public int capacity;
public double latitude;
public double longtitude;
int free=0;
int taken=0;
int id;
public Parking(){
}
public Parking(int capacity,double latitude,String location, double longtitude,String name,String zone){
this.capacity=capacity;
this.location=location;
this.name=name;
this.zone=zone;
this.latitude=latitude;
this.longtitude=longtitude;
this.taken=getTaken();
this.free=getFree();
}
protected Parking(Parcel in) {
name = in.readString();
zone = in.readString();
location = in.readString();
capacity = in.readInt();
latitude = in.readDouble();
longtitude = in.readDouble();
free = in.readInt();
taken = in.readInt();
}
public static final Creator<Parking> CREATOR = new Creator<Parking>() {
@Override
public Parking createFromParcel(Parcel in) {
return new Parking(in);
}
@Override
public Parking[] newArray(int size) {
return new Parking[size];
}
};
public int getCapacity() {
return capacity;
}
public int getFree() {
return capacity-getTaken();
}
public int getTaken() {
Random random=new Random();
this.taken=random.nextInt(capacity+1);
return taken;
}
public void setFree(int taken){
free=capacity-taken;
}
public void setTaken(int taken) {
Random random=new Random();
this.taken=random.nextInt(capacity+1);
}
public double getLatitude(){
return latitude;
}
public double getLongtidue(){
return longtitude;
}
@Override
public String toString() {
return String.format("%s\t\tZone: %s\nlocation: %s\nCapacity: %d\t\tFree: %d",name,zone,location,capacity,getFree());
}
@Override
public int compareTo(@NonNull Parking parking) {
return Integer.compare(parking.free,this.free);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(capacity);
parcel.writeDouble(latitude);
parcel.writeString(location);
parcel.writeDouble(longtitude);
parcel.writeString(name);
parcel.writeString(zone);
parcel.writeInt(getTaken());
parcel.writeInt(getFree());
}
}
我與火力地堡連接它,安裝谷歌-services.json文件(通過使用火力工具),但是當我開始崩潰的應用程序(「不幸的是,najdiparking(我的應用程序名稱)已經停止) 。當我手動填寫列表(不從數據庫讀取)時,應用程序可以正常工作。 這是依賴關係的com.google.firebase:firebase-database:10.0.1。
我也檢查我的.json文件名與類中的名稱。
我logcat的(我希望這將幫助你)
08-27 15:41:31.884 19384-19384/com.example.simeon.najdiparking E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.simeon.najdiparking, PID: 19384 java.lang.NoSuchMethodError: No virtual method zzTt()Z in class Lcom/google/firebase/FirebaseApp; or its super classes (declaration of 'com.google.firebase.FirebaseApp' appears in /data/app/com.example.simeon.najdiparking-1/split_lib_dependencies_apk.apk:classes20.dex) at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source) at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source) at com.example.simeon.najdiparking.MainActivity.onCreate(MainActivity.java:48) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
您可以提供日誌以更好地瞭解您的問題 – Sergey
發佈logcat錯誤。 –
看起來您複製了logcat的錯誤部分。 –