所以,奇怪的事情发生了。我正在使用以下代码使用gson将json序列化为User类:
User user = gson.fromJson(json, User.class);
这是我的用户类别:
@Entity(tableName = "user")
public class User implements Serializable {
@PrimaryKey(autoGenerate = true)
private long id;
private String fullName;
private String emailId;
private String deviceId;
private String trialId;
private String macAddress;
private String timestamp = new SimpleDateFormat("YYYY-MM-dd HH:mm",
Locale.getDefault()).format(Calendar.getInstance().getTime());
public User() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public String getMacAddress() {
return macAddress;
}
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getTrialId() {
return trialId;
}
public void setTrialId(String trialId) {
this.trialId = trialId;
}
}
在我一直在Android 6上运行该应用程序并开始收到以下异常之前,这种方法一直运行良好。
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): Failed to handle method call
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): java.lang.RuntimeException: Failed to invoke public org.iprd.hiv_subjective_assessment.models.User() with no args
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:118)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at com.google.gson.Gson.fromJson(Gson.java:927)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at com.google.gson.Gson.fromJson(Gson.java:892)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at com.google.gson.Gson.fromJson(Gson.java:841)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at com.google.gson.Gson.fromJson(Gson.java:813)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at org.iprd.wits_subjective_assessment.MainActivity.saveUser(MainActivity.java:132)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at org.iprd.wits_subjective_assessment.MainActivity.lambda$onCreate$0$MainActivity(MainActivity.java:75)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at org.iprd.wits_subjective_assessment.-$$Lambda$MainActivity$3-yCJcZn0XmWJfjUPHlTfbHTdCg.onMethodCall(lambda)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:643)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at android.os.MessageQueue.next(MessageQueue.java:323)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at android.os.Looper.loop(Looper.java:135)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at android.app.ActivityThread.main(ActivityThread.java:7399)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): Caused by: java.lang.IllegalArgumentException: Unknown pattern character 'Y'
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at java.text.SimpleDateFormat.validatePatternCharacter(SimpleDateFormat.java:323)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at java.text.SimpleDateFormat.validatePattern(SimpleDateFormat.java:305)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at java.text.SimpleDateFormat.<init>(SimpleDateFormat.java:365)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at org.iprd.hiv_subjective_assessment.models.User.<init>(User.java:23)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at java.lang.reflect.Constructor.newInstance(Native Method)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:110)
E/MethodChannel#org.iprd.wits_hiv_subjective_assessment/hiv_assessment(28371): ... 18 more
我不确定世界上正在发生什么。格森不适合棉花糖吗?