我正在用firebase DB编写android应用程序,并在android api 22上对其进行测试,并且可以正常工作,但是当我尝试在api 26(小米mi 6)上启动它时,它将终止。我猜问题出在Gson()。fromJson()。
api 26错误:
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.easyshop, PID: 10051
com.google.gson.JsonSyntaxException: Jul 17, 2018 11:35:06
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:87)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.example.user.easyshop.MainActivity$1.onDataChange(MainActivity.java:64)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
Caused by: java.text.ParseException: Failed to parse date ["Jul 17, 2018 11:35:06"]: Invalid number: Jul
at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:274)
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:85)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.example.user.easyshop.MainActivity$1.onDataChange(MainActivity.java:64)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
Caused by: java.lang.NumberFormatException: Invalid number: Jul
at com.google.gson.internal.bind.util.ISO8601Utils.parseInt(ISO8601Utils.java:311)
at com.google.gson.internal.bind.util.ISO8601Utils.parse(ISO8601Utils.java:129)
at com.google.gson.internal.bind.DateTypeAdapter.deserializeToDate(DateTypeAdapter.java:85)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:75)
at com.google.gson.internal.bind.DateTypeAdapter.read(DateTypeAdapter.java:46)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at com.example.user.easyshop.MainActivity$1.onDataChange(MainActivity.java:64)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6665)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
I/Process: Sending signal. PID: 10051 SIG: 9
Application terminated.
(com.example.user.easyshop.MainActivity $ 1.onDataChange(MainActivity.java:64)at)关于我的代码的此错误:
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Type listType = new TypeToken<ArrayList<Item>>(){}.getType();
List<Item> yourClassList = new Gson().fromJson(dataSnapshot.getValue(String.class), listType); //this line
Config.LIST = (ArrayList<Item>) yourClassList;
}
我的Item.class:
public class Item implements Comparable<Item>{
private String name;
private boolean bought;
private boolean deleted;
private Date addDate;
private Date operationDate;
public Item(){
this.name = "Item";
bought = false;
deleted = false;
addDate = new Date();
operationDate = null;
}
public Item(String name){
this.name = name;
bought = false;
deleted = false;
addDate = new Date();
operationDate = null;
@Override
public int compareTo(@NonNull Item o) {
int actual = isBought() ? 1 : 0;
int other = o.isBought() ? 1 : 0;
if(actual == other)
return name.compareTo(o.name);
else
return actual - other;
}
...
}
正如我所说,一切都可以在其他设备上运行。会是什么?
答案 0 :(得分:1)
Caused by: java.text.ParseException: Failed to parse date ["Jul 17, 2018 11:35:06"]: Invalid number: Jul
.....
Caused by: java.lang.NumberFormatException: Invalid number: Jul
您可以将dateFormat模板用于日期解析示例:
Gson gson = new GsonBuilder()
.setDateFormat("MM dd, yyyy HH:mm:ss")
.create();
https://developer.android.com/reference/java/text/SimpleDateFormat
答案 1 :(得分:0)
Caused by: java.text.ParseException: Failed to parse date ["Jul 17, 2018 11:35:06"]: Invalid number: Jul
.....
Caused by: java.lang.NumberFormatException: Invalid number: Jul
您正在尝试将“ Jul”解析为数字。