我尝试将我的应用程序连接到Firebase数据库,但每当我启动显示问题的代码时
这是我的主要活动
public class MainActivity extends AppCompatActivity {
private static final String TAG = "ViewDatabase";
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myRef;
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference("Discount/Business");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
UserInformation uInfo = new UserInformation();
uInfo.setAltro(ds.child("Altro").getValue(UserInformation.class).getAltro());
uInfo.setCognome(ds.child("Cognome").getValue(UserInformation.class).getCognome());
uInfo.setInformazioni(ds.child("Informazioni").getValue(UserInformation.class).getInformazioni());
uInfo.setNome(ds.child("Nome").getValue(UserInformation.class).getNome());
Log.d(TAG, "showData: Altro: " + uInfo.getAltro());
Log.d(TAG, "showData: Cognome: " + uInfo.getCognome());
Log.d(TAG, "showData: Informazioni: " + uInfo.getInformazioni());
Log.d(TAG, "showData: Nome: " + uInfo.getNome());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
这是我的用户类
public class UserInformation {
private String Altro;
private String Cognome;
private String Informazioni;
private String Nome;
public UserInformation(){
}
public String getAltro() {
return Altro;
}
public void setAltro(String altro) {
Altro = altro;
}
public String getCognome() {
return Cognome;
}
public void setCognome(String cognome) {
Cognome = cognome;
}
public String getInformazioni() {
return Informazioni;
}
public void setInformazioni(String informazioni) {
Informazioni = informazioni;
}
public String getNome() {
return Nome;
}
public void setNome(String nome) {
Nome = nome;
}
和我的日志错误
11-14 10:59:55.016 2909-2909/com.app8 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app8, PID: 2909
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type com.app8.UserInformation
at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.app8.MainActivity$1.onDataChange(MainActivity.java:41)
at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
at com.google.android.gms.internal.zzboc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这是我第一次使用Firebase,我已经看到了与该主题相关的其他问题,但我无法弄清楚如何使其工作,我希望你能帮助我。
感谢您的帮助,我们非常感谢任何建议
答案 0 :(得分:1)
要从数据库中获取这些值,请使用以下代码:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference businessRef = rootRef.child("Discount").child("Business");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String altro = ds.child("Altro").getValue(String.class);
String cognome = ds.child("Cognome").getValue(String.class);
String informazioni = ds.child("Informazioni").getValue(String.class);
String nome = ds.child("Nome").getValue(String.class);
Log.d("TAG", altro + " / " + cognome + " / " + informazioni + " / " + nome);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
businessRef.addListenerForSingleValueEvent(eventListener);
作为建议,根据cu Java Naming Conventions将来使用所有字段。
答案 1 :(得分:0)
你能在下面试试。
UserInformation uiFromSnapShot = ds.getValue(UserInformation.class); uInfo.setXXX(uiFromSnapShot.getXXX);
答案 2 :(得分:0)
我弄清楚了此错误的实际原因。我刚才也遇到了这个问题。我了解到,firebase数据库中发生了某种垃圾数据收集。
解决方案: 打开Firebase控制台->转到数据库->删除所有数据库数据->从手机上卸载应用程序->重新安装->错误已解决!!
快乐编码!