Firebase包含列表时,应用程序崩溃

时间:2018-09-13 11:23:38

标签: java android firebase firebase-realtime-database

我有一个应用程序,它可以保存Firebase当前位置(城市名称等),ID和带有点的列表,以便我可以保存折线。在firebase中成功导入。问题是,当我转到另一个包含包含Firebase所有城市名称的列表视图的活动时,应用程序崩溃了。我试图使它没有Firebase中的列表,并且工作正常。因此,只有在Firebase包含带有点的列表时,该问题才会存在。

这是我的基地的一个例子。在这里,“ ola”是包含点的列表,我仅在最后一个点有它。如果我删除了最后一个,则该应用程序可以正常工作。

enter image description here

import java.util.List;




public class Paths {
String id;
String name;
String city;
String nomos;
String perifereia;
List<Ola> ola;



public Paths(){

}



public Paths(String id, String name, String city, String nomos, String perifereia, List<Ola> ola) {
    this.id = id;
    this.name = name;
    this.city = city;
    this.nomos = nomos;
    this.perifereia = perifereia;
    this.ola = ola;

}

public String getId() {
    return id;
}

public String getName() {
    return name;
}

public String getCity() {
    return city;
}

public String getNomos() {
    return nomos;
}

public String getPerifereia() {
    return perifereia;
}

public List<Ola> getOla() {
    return ola;
}

public class Ola {

public double latitude;
public double longitude;


public Ola() {}


public Ola(double latitude, double longitude) {
    this.latitude = latitude;
    this.longitude = longitude;
}

public double getLatitude() {
    return latitude;
}

public void setLatitude(double latitude) {
    this.latitude = latitude;
}

public double getLongitude() {
    return longitude;
}

public void setLongitude(double longitude) {
    this.longitude = longitude;
}

}

这是错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.spiros.mypath, PID: 28814
              com.google.firebase.database.DatabaseException: Can't convert object of type java.util.ArrayList to type com.example.spiros.mypath.Ola
                  at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
                  at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
                  at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
                  at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
                  at com.google.android.gms.internal.firebase_database.zzku.zza(Unknown Source)
                  at com.google.android.gms.internal.firebase_database.zzkt.zzb(Unknown Source)
                  at com.google.android.gms.internal.firebase_database.zzkt.zza(Unknown Source)
                  at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                  at com.example.spiros.mypath.CitiesList$1.onDataChange(CitiesList.java:44)
                  at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
                  at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
                  at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
                  at android.os.Handler.handleCallback(Handler.java:761)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  at android.os.Looper.loop(Looper.java:156)
                  at android.app.ActivityThread.main(ActivityThread.java:6595)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

这是从firebase接收数据的代码:

    databasePaths = FirebaseDatabase.getInstance().getReference("Paths");

    databasePaths.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot items: dataSnapshot.getChildren())
            {
                info = items.getValue(Paths.class);
                ArrayListCities.add(info.city);

            }


            adapterCities = new ArrayAdapter<>(CitiesList.this, android.R.layout.simple_list_item_1, ArrayListCities);
            ListViewCities.setAdapter(adapterCities);
            adapterCities.notifyDataSetChanged();


            ListViewCities.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String selectedFromListCities = (ListViewCities.getItemAtPosition(position).toString());



                    Intent IntentGoN = new Intent(getApplicationContext(), FullListByCity.class);
                    IntentGoN.putExtra("city", selectedFromListCities);
                    startActivity(IntentGoN);

                }
            });

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

1 个答案:

答案 0 :(得分:1)

这是问题

Class java.util.List has generic type parameters, please use GenericTypeIndicator instead

需要告知列表其持有的是哪种对象。因此,您将需要创建一个可以接收变量ola

的模型类。

首先确保您的Firebase数据库结构为

key-|
---city
---....
---ola|
-------0|
---------latitude
---------longitude
-------1|
---------...

然后使用构造函数为ola创建带有两个对象(纬度和经度)的模型类

现在您可以将列表用作

List<Ola> ola;