我正在做一个基于firebase的android应用程序。我的日志显示数据更改错误。代码如下:
myRef.child("first").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
model model = childSnapshot.getValue(model.class);
if (dataSnapshot.getValue() == null) {
Toast.makeText(getApplicationContext(),"Sorry",Toast.LENGTH_SHORT).show();
} else {
String name = model.getFb01name();
tvName.setText(name);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
有人能改进吗?
这是我的logcat错误消息:它显示了这一行的错误。
model model = childSnapshot.getValue(model.class);
“在我的\项目\名称.sheetactivity$3.ondatachange(sheetactivity。java:156)"
2020-11-23 17:41:35.403 5585-5585/my_project_name E/AndroidRuntime: FATAL EXCEPTION: main
Process: my_project_name, PID: 5585
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Long to type my_project_name.Model.model
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
at my_project_name.SheetActivity$3.onDataChange(SheetActivity.java:156)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
我试了很多。有时它工作了,但又停止了。我不能指出错误在哪里。
我的模型课如下:
public class model {
private String fb01name;
public model() {
}
public model(String fb01name) {
this.fb01name = fb01name;
}
public String getFb01name() {
return fb01name;
}
public void setFb01name(String fb01name) {
this.fb01name = fb01name;
}
}
我的火基结构是这样的:
这是应用建议后的最新logcat消息:
2020-11-24 00:40:06.783 12399-12399/my_project_nameE/AndroidRuntime: FATAL EXCEPTION: main
Process: my_project_name, PID: 12399
com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(CustomClassMapper.java:426)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:217)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:179)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(CustomClassMapper.java:48)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
at my_project_name.SheetActivity$3.onDataChange(SheetActivity.java:155)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2条答案
按热度按时间i5desfxk1#
在这里试试这个,如果您的模型类只有一个参数,这应该是好的。如果需要数据库中的所有参数,则需要从数据库中提取所有参数,然后将其添加到模型中。
4ngedf3f2#
自从你读了
first
,的DataSnapshot
你能进去吗onDataChange
包含该位置的数据。然后循环快照中的子对象,这样childSnapshot
变量然后指向first
(所以fb00max
,fb01mark1
等)。当你试着读一本model
对象,它将作为值失败10
不能解释为model
.你要么从树的更高一级读取,要么从你的文件中删除循环
onDataChange
.从树的更高一级读取
从ondatachange中删除循环