问题描述
我每次执行的时候都会收到stacktrace错误。
GenericTypeIndicator< List< String>> t = new GenericTypeIndicator< List< String>>(){
};
GenericTypeIndicator< List< String>> t2 = new GenericTypeIndicator< List< String>>(){};
列表< String> namelist = snapshot.child(Driver name)。getValue(t);
列表< String> carlist = snapshot.child(Drivers car)。getValue(t2);
if(namelist!= null& carlist!= null){
for(int wee = 0; wee driver.add(new Driver(namelist.get(wee).toString(),carlist.get(wee).toString()));
}
我发送给firebase的数据存储为:
$ b$ b $ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ -Js7GjPMGNOLurrKk9rC:iggvv,
-Js7GlUlCwIoljJxpFbZ:iiggv,
-Js7GnBCRogiFUfn-ncZ:ihccvv,
-Js7GqGxH2vLYK5tsjjP:hhvv,
-Js7Gs80AoB5eeWci6jW:hhgc,
-Js7Gtm6AGh_QaTUK6fQ:ugghh,
-Js7GyomOIzjtVo3-PVg:tyygff,
-JsPxEhf4tx7O4v7JWMc: ,
-JsQBoMqVBF4SzOHF3WA:rr,
-JsQC6TuGqHrBBkdalik:eed
},
驱动程序可用性:{
- Js6OJozSuUvj5JnuNnW:否,
-Js7GjQ_5YgmnEYKWHrL:否,
-Js7GlVm7onoQd59Yyfq:否,
-Js7GnDPdjseoRVjgwSn:否,
-Js7GqIYy4VSEqu9qZfC:否,
-Js7Gs8VP5mp5bOR1pxR:否,
-Js7Gtmfv2M2p201k3Ra:否,
-Js7Gyvf8JkGwBlh8ZLu:否,
-JsPxEly9iGncA4VWvn2:否,
-JsQBoOE6AbGy1VMBSWp:否,
-JsQC6wGzBXBMz76IXue:否
Drivers car:{
-Js6OJoyDlc4DR0IQhJr:gvgh,
-Js7GjQZ6Sb7AOooABt3:nbcgg,
-Js7GlVajAA1B5PeBnn2:nvvuuu ,
-Js7GnDOCiWM874f6iL6:,
-Js7GqIYy4VSEqu9qZfB:nbcxx,
-Js7Gs8VP5mp5bOR1pxQ:hbccf,
-Js7Gtmfv2M2p201k3R_: hvccf,
-Js7Gyvf8JkGwBlh8ZLt:gghhhg,
-JsPxElx9XXm0kVLFstN:,
-JsQBoOE6AbGy1VMBSWo:dd,
-JsQC6wF0zgLApv8SwJO:
堆栈跟踪出错:
06-30 14:35:44.918 2736-2736 / com.kate.teme E / AndroidRuntime:FATAL EXCEPTION:main
com.firebase.client .FirebaseException:无法弹出键入
在com.firebase.client.DataSnapshot.getValue(DataSna pshot.java:208)
在com.kate.teme.AdminContent $ 2.onDataChange(AdminContent.java:105)
在com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45 )
在com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
在com.firebase.client.core.view.EventRaiser $ 1.run(EventRaiser.java: 38)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper .loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
在java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android .internal.os.ZygoteInit.main(ZygoteInit.java:600)
在dalvik.system.NativeStart.main(本地方法)
引起:com.shaded.fasterxml.jackson.databind.JsonMappingException:不能反序列化的实例java.util.ArrayList出START_OBJECT令牌
at [源代码:java.io.StringReader@42af1900; line:1,column:1]
at com.shaded.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
at com.shaded.fasterxml.jackson.databind.DeserializationContext。 mappingException(DeserializationContext.java:569)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.handleNonArray(StringCollectionDeserializer.java:218)
at com.shaded.fasterxml.jackson。 databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:166)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:156)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:19)
at com.shaded.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.shaded.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2041)
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:206)
at com.kate .teme.AdminContent $ 2.onDataChange(AdminContent.java:105)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core。 view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser $ 1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback (Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Nativ e方法)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
查看您的数据结构:
Driver name:{
-Js6OJniEqVZAYSNhqbB:gvvb,
-Js7GjPMGNOLurrKk9rC:iggvv,
-Js7GlUlCwIoljJxpFbZ:iiggv ,
-Js7GnBCRogiFUfn-ncZ:ihccvv,
-Js7GqGxH2vLYK5tsjjP:hhvv,
-Js7Gs80AoB5eeWci6jW:hhgc,
-Js7Gtm6AGh_QaTUK6fQ :ugghh,
-Js7GyomOIzjtVo3-PVg:tyygff,
-JsPxEhf4tx7O4v7JWMc:,
-JsQBoMqVBF4SzOHF3WA:rr,
-JsQC6TuGqHrBBkdalik:eed
}
Y你试着将它反序列化为 List< String>
。但实际上它是一个 Map< String,String>
。所以:
$ b $ pre $ @Override
public void onDataChange(DataSnapshot snapshot){
GenericTypeIndicator< Map< String,串GT;> mapType = new GenericTypeIndicator< Map< String,String>>(){};
Map< String,String> namelist = snapshot.child(Driver name)。getValue(mapType);
Map< String,String> carlist = snapshot.child(Drivers car)。getValue(mapType); (namelist:= null& carlist!= null){
for(String name:namelist.values()){
System.out.println(name);
$ / code $ / pre
$ b $注意我不知道你怎么试图匹配一个司机和他们的车。我强烈建议你在继续编码之前重新考虑这个数据结构。
一个可能的结构:
DriversList:{
-Js6OJniEqVZAYSNhqbB:{
name:puf,
available:
car:Toyota RAV4
},
-Js7GjPMGNOLurrKk9rC:{
name:manon,
available:
car:丰田矩阵
读/写这些,你需要一个 Driver
class:
public class Driver {
String name;
布尔可用;
弦乐车;
public Driver(){}
public String getName(){return name; }
public Boolean getAvailable(){return available; }
public String getCar(){return car; }
}
然后:
public void onDataChange(DataSnapshot snapshot){
GenericTypeIndicator< Map< String,Driver>> mapType = new GenericTypeIndicator< Map< String,Driver>>(){};
地图< String,Driver> drivers = snapshot.getValue(mapType); $(驱动程序!= null){
为(驱动程序driver:drivers.values()){
System.out.println(driver.name);
}
}
}
I keep getting the stacktrace error below each time I implement
GenericTypeIndicator<List<String>> t = new GenericTypeIndicator<List<String>>() {
};
GenericTypeIndicator<List<String>> t2 = new GenericTypeIndicator<List<String>>() {};
List<String> namelist = snapshot.child("Driver name").getValue(t);
List<String> carlist = snapshot.child("Drivers car").getValue(t2);
if (namelist!=null & carlist!=null){
for(int wee=0;wee<namelist.size();wee++){
driver.add(new Driver(namelist.get(wee).toString(),carlist.get(wee).toString()));
}
The data I sent to firebase is stored as:
"DriversList" : {
"Driver name" : {
"-Js6OJniEqVZAYSNhqbB" : "gvvb",
"-Js7GjPMGNOLurrKk9rC" : "iggvv",
"-Js7GlUlCwIoljJxpFbZ" : "iiggv",
"-Js7GnBCRogiFUfn-ncZ" : "ihccvv",
"-Js7GqGxH2vLYK5tsjjP" : "hhvv",
"-Js7Gs80AoB5eeWci6jW" : "hhgc",
"-Js7Gtm6AGh_QaTUK6fQ" : "ugghh",
"-Js7GyomOIzjtVo3-PVg" : "tyygff",
"-JsPxEhf4tx7O4v7JWMc" : "",
"-JsQBoMqVBF4SzOHF3WA" : "rr",
"-JsQC6TuGqHrBBkdalik" : "eed"
},
"Drivers availability" : {
"-Js6OJozSuUvj5JnuNnW" : "No",
"-Js7GjQ_5YgmnEYKWHrL" : "No",
"-Js7GlVm7onoQd59Yyfq" : "No",
"-Js7GnDPdjseoRVjgwSn" : "No",
"-Js7GqIYy4VSEqu9qZfC" : "No",
"-Js7Gs8VP5mp5bOR1pxR" : "No",
"-Js7Gtmfv2M2p201k3Ra" : "No",
"-Js7Gyvf8JkGwBlh8ZLu" : "No",
"-JsPxEly9iGncA4VWvn2" : "No",
"-JsQBoOE6AbGy1VMBSWp" : "No",
"-JsQC6wGzBXBMz76IXue" : "No"
},
"Drivers car" : {
"-Js6OJoyDlc4DR0IQhJr" : "gvgh",
"-Js7GjQZ6Sb7AOooABt3" : "nbcgg",
"-Js7GlVajAA1B5PeBnn2" : "nvvuuu",
"-Js7GnDOCiWM874f6iL6" : "",
"-Js7GqIYy4VSEqu9qZfB" : "nbcxx",
"-Js7Gs8VP5mp5bOR1pxQ" : "hbccf",
"-Js7Gtmfv2M2p201k3R_" : "hvccf",
"-Js7Gyvf8JkGwBlh8ZLt" : "gghhhg",
"-JsPxElx9XXm0kVLFstN" : "",
"-JsQBoOE6AbGy1VMBSWo" : "dd",
"-JsQC6wF0zgLApv8SwJO" : "ff"
}
Error from stack trace:
06-30 14:35:44.918 2736-2736/com.kate.teme E/AndroidRuntime﹕ FATAL EXCEPTION: main
com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:208)
at com.kate.teme.AdminContent$2.onDataChange(AdminContent.java:105)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.shaded.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
at [Source: java.io.StringReader@42af1900; line: 1, column: 1]
at com.shaded.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
at com.shaded.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:569)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.handleNonArray(StringCollectionDeserializer.java:218)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:166)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:156)
at com.shaded.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:19)
at com.shaded.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.shaded.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2041)
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:206)
at com.kate.teme.AdminContent$2.onDataChange(AdminContent.java:105)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:800)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
解决方案 Looking at your data structure:
"Driver name" : {
"-Js6OJniEqVZAYSNhqbB" : "gvvb",
"-Js7GjPMGNOLurrKk9rC" : "iggvv",
"-Js7GlUlCwIoljJxpFbZ" : "iiggv",
"-Js7GnBCRogiFUfn-ncZ" : "ihccvv",
"-Js7GqGxH2vLYK5tsjjP" : "hhvv",
"-Js7Gs80AoB5eeWci6jW" : "hhgc",
"-Js7Gtm6AGh_QaTUK6fQ" : "ugghh",
"-Js7GyomOIzjtVo3-PVg" : "tyygff",
"-JsPxEhf4tx7O4v7JWMc" : "",
"-JsQBoMqVBF4SzOHF3WA" : "rr",
"-JsQC6TuGqHrBBkdalik" : "eed"
}
You try to deserialize this into a List<String>
. But it actually is a Map<String,String>
. So:
@Override
public void onDataChange(DataSnapshot snapshot) {
GenericTypeIndicator<Map<String,String>> mapType = new GenericTypeIndicator<Map<String, String>>() { };
Map<String,String> namelist = snapshot.child("Driver name").getValue(mapType);
Map<String,String> carlist = snapshot.child("Drivers car").getValue(mapType);
if (namelist!=null & carlist!=null) {
for (String name: namelist.values()) {
System.out.println(name);
}
}
}
Note that I have no idea how you're trying to match up a driver and their car. I highly recommend that you reconsider this data structure before you continue to code.
One possible structure:
"DriversList": {
"-Js6OJniEqVZAYSNhqbB" : {
"name": "puf",
"available": false,
"car": "Toyota RAV4"
},
"-Js7GjPMGNOLurrKk9rC" : {
"name": "manon",
"available": false,
"car": "Toyota Matrix"
},
...
}
To read/write these, you'll need a Driver
class:
public class Driver {
String name;
Boolean available;
String car;
public Driver() {}
public String getName() { return name; }
public Boolean getAvailable() { return available; }
public String getCar() { return car; }
}
And then:
public void onDataChange(DataSnapshot snapshot) {
GenericTypeIndicator<Map<String,Driver>> mapType = new GenericTypeIndicator<Map<String, Driver>>() { };
Map<String,Driver> drivers = snapshot.getValue(mapType);
if (drivers!=null) {
for (Driver driver: drivers.values()) {
System.out.println(driver.name);
}
}
}
这篇关于从Firebase中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!