我一直在尝试从firebase数据库更新,但每次我这样做都会给我一个错误...这是我的Firebase数据库:
mDatabaseGroup.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String team_a = (String) dataSnapshot.child("Team_A").child("team_name").getValue();
final long team_ad = (long) dataSnapshot.child("Team_A").child("d").getValue();
final long team_al = (long) dataSnapshot.child("Team_A").child("l").getValue();
final long team_ap = (long) dataSnapshot.child("Team_A").child("p").getValue();
final long team_aw = (long) dataSnapshot.child("Team_A").child("w").getValue();
final long team_apts = (long) dataSnapshot.child("Team_A").child("pts").getValue();
final String team_b = (String) dataSnapshot.child("Team_B").child("team_name").getValue();
final long team_bd = (long) dataSnapshot.child("Team_B").child("d").getValue();
final long team_bl = (long) dataSnapshot.child("Team_B").child("l").getValue();
final long team_bp = (long) dataSnapshot.child("Team_B").child("p").getValue();
final long team_bw = (long) dataSnapshot.child("Team_B").child("w").getValue();
final long team_bpts = (long) dataSnapshot.child("Team_B").child("pts").getValue();
final String team_c = (String) dataSnapshot.child("Team_C").child("team_name").getValue();
String team_d = (String) dataSnapshot.child("Team_D").child("team_name").getValue();}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是我遇到的错误:
ClassCastException:java.lang.String无法强制转换为java.lang.Long
这是我的stackTrace:
D/NativeCrypto: ssl=0x56944ba8 sslWrite buf=0x427180f8 len=99 write_timeout_millis=0
D/NativeCrypto: sslNotify, appData=0x5807ff10 ret=1
D/NativeCrypto: sslSelect, appData=0x5807ff10 woken up by a token
D/NativeCrypto: sslSelect, appData=0x5807ff10 read ret=1
D/SQLiteDatabase: beginTransaction()
D/SQLiteDatabase: endTransaction()
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4187ece0)
W/dalvikvm: threadid=1: uncaught exception occurred
W/System.err: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
W/System.err: at com.example.zack.admin.ViewFixtureActivity$1.onDataChange(ViewFixtureActivity.java:85)
W/System.err: at com.google.android.gms.internal.zzaih.zza(Unknown Source)
W/System.err: at com.google.android.gms.internal.zzajh.zzctc(Unknown Source)
W/System.err: at com.google.android.gms.internal.zzajk$1.run(Unknown Source)
W/System.err: at android.os.Handler.handleCallback(Handler.java:808)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:103)
W/System.err: at android.os.Looper.loop(Looper.java:193)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5299)
W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
W/System.err: at dalvik.system.NativeStart.main(Native Method)
W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zack.admin, PID: 26018
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
at com.example.zack.admin.ViewFixtureActivity$1.onDataChange(ViewFixtureActivity.java:85)
at com.google.android.gms.internal.zzaih.zza(Unknown Source)
at com.google.android.gms.internal.zzajh.zzctc(Unknown Source)
at com.google.android.gms.internal.zzajk$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
如果有人知道另一种在firebase上更新的方式,也欢迎。
答案 0 :(得分:1)
如果您想读取类型为long的单个值(即/ Groups / -Kh2GRPKiuQdePCubLi0 / A / Team_A / d),您可以编写如下代码:
FirebaseDatabase.getInstance().getReference("Groups/-Kh2GRPKiuQdePCubLi0/A/Team_A/d").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Long team_ad = dataSnapshot.getValue(Long.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
如果从参考文献中读取(Groups / -Kh2GRPKiuQdePCubLi0 / A),您可以编写如下代码:
// Create a model class for your dataSnapshot
public static class Team {
public long d;
public long l;
public long p;
public long pts;
public String team_name;
public long w;
public Team() {
// Default constructor required for calls to DataSnapshot.getValue(Team.class)
}
}
// Read from database
FirebaseDatabase.getInstance().getReference("Groups/-Kh2GRPKiuQdePCubLi0/A").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot childDataSnapShot: dataSnapshot.getChildren()){
Team team = childDataSnapShot.getValue(Team.class);
switch (childDataSnapShot.getKey())
{
case "Team_A":
final String team_a = team.team_name;
final long team_ad = team.d;
final long team_al = team.l;
final long team_ap = team.p;
final long team_aw = team.w;
final long team_apts = team.pts;
break;
case "Team_B":
final String team_b = team.team_name;
final long team_bd = team.d;
final long team_bl = team.l;
final long team_bp = team.p;
final long team_bw = team.w;
final long team_bpts = team.pts;
break;
case "Team_C":
final String team_c = team.team_name;
final long team_cd = team.d;
final long team_cl = team.l;
final long team_cp = team.p;
final long team_cw = team.w;
final long team_cpts = team.pts;
break;
default:
break;
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Team failed, log a message
Log.w(LOG_TAG, "loadTeam:onCancelled ", databaseError.toException());
}
});
}
参考:https://firebase.google.com/docs/database/android/read-and-write#read_and_write_data
答案 1 :(得分:0)
String.valueOf()
进行投射,请在该位置使用(String)
String.valueOf(dataSnapshot.child("Team_C").child("team_name").getValue());
答案 2 :(得分:0)
首先,确保您的DatabaseReference mDatabaseGroup
位于/Groups/Key/A
的正确位置,以便从数据库中获取正确的值。
其次,试试
final long team_ad = dataSnapshot.child("Team_A").child("d").getValue(Long.class);