[英]String to Localdate DateTimeParseException
I'm loading a string from SQLite database, then trying to convert it to a LocalDate so I can add it as an object parameter, then compare the date of objects on a list. 我正在从SQLite数据库加载字符串,然后尝试将其转换为LocalDate,以便可以将其添加为对象参数,然后比较列表上对象的日期。
But the app crashes and gives DateTimeParseException. 但是该应用程序崩溃并给出了DateTimeParseException。
This is the method that crashes, and it crashes at the line where I'm creating the LocalDate. 这是崩溃的方法,它在我创建LocalDate的行崩溃。
I have found and read many similar threads on this site and others too, but sadly I couldn't figure out what is the problem exactly. 我已经在该站点以及其他站点上找到并阅读了许多类似的主题,但可悲的是我无法弄清楚到底是什么问题。
private void fillEventsArray(){
Cursor data = mDatabaseHelper.getData();
while(data.moveToNext()){
String name = data.getString(1);
String description = data.getString(2);
String time = data.getString(3);
String date = data.getString(4);
LocalDate localDate= LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy/MM/dd"));
LocalTime localTime=LocalTime.parse(time, DateTimeFormatter.ofPattern("HH:mm"));
Event event = new Event(name, description, localTime, localDate);
events.add(event);
}
I'm saving data in column 4 in the same "yyyy/mm/dd" format, like 2018/11/20. 我将以相同的“ yyyy / mm / dd”格式将数据存储在第4列中,例如2018/11/20。
Previously I also tryed turning the strings into LodalDate/LocalTime like this, but it had the same problems: 以前,我还尝试过将字符串转换为LodalDate / LocalTime,如下所示,但存在相同的问题:
String year=date.substring(0,4);
String month=date.substring(5,7);
String day=date.substring(8,10);
LocalDate localDate=LocalDate.of(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(day));
String hour=time.substring(0,3);
String minute=time.substring(4,5);
LocalTime localTime=LocalTime.of(Integer.parseInt(hour),Integer.parseInt(minute));
Does it cause an error if the database is empty? 如果数据库为空,是否会导致错误?
If any more information is required let me know and I'll provide it. 如果需要更多信息,请告诉我,我会提供。
Logcat copy: Logcat副本:
2018-11-12 15:48:57.836 12038-12038/com.example.dflavio.calendar E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dflavio.calendar, PID: 12038
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dflavio.calendar/com.example.dflavio.calendar.MainActivity}: java.time.format.DateTimeParseException: Text 'android.support.v7.widget.AppCompatEditText{ceb966d VFED..CL. .F...' could not be parsed at index 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
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:767)
Caused by: java.time.format.DateTimeParseException: Text 'android.support.v7.widget.AppCompatEditText{ceb966d VFED..CL. .F...' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.time.LocalDate.parse(LocalDate.java:394)
at com.example.dflavio.calendar.MainActivity.fillEventsArray(MainActivity.java:104)
at com.example.dflavio.calendar.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
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:767)
As somebody have pointed out I might have made a mistake at how I get the strings from my edittexts. 正如有人指出的那样,我如何从编辑文本中获取字符串可能犯了一个错误。 Currently this is how the code looks:
当前,代码如下所示:
createBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText nameEditText = (EditText) findViewById(R.id.nameEditText);
EditText descriptionEditText = (EditText) findViewById(R.id.descriptionEditText);
EditText timeEditText = (EditText) findViewById(R.id.timeEditText);
EditText dateEditText = (EditText) findViewById(R.id.dateEditText);
String name = nameEditText.getText().toString();
String description = descriptionEditText.getText().toString();
String time = timeEditText.getText().toString();
String date = dateEditText.getText().toString();
if (name.length() != 0){
if (description.length() != 0){
if (time.length() != 0){
if (date.length() != 0){
AddData(name, description, time, date);
nameEditText.setText("");
descriptionEditText.setText("");
timeEditText.setText("");
dateEditText.setText("");
}
}
}
}else{
toastMessage("Fill in all fields.");
}
Thank you for your help in advance. 提前谢谢你的帮助。
Your DB seems to contain garbage; 您的数据库似乎包含垃圾; you try to parse a string like "android.support.v7.widget.AppCompatEditText{ceb966d VFED..CL. .F..." as a date.
您尝试将类似“ android.support.v7.widget.AppCompatEditText {ceb966d VFED..CL。.F ...”的字符串作为日期进行解析。
You are incorrectly retrieving text from your Edit texts by calling nameEditText.toString()
, which will try to convert the EditText object to a string, as you can see from the parse error. 您通过调用
nameEditText.toString()
从编辑文本中错误地检索了文本,这将尝试将EditText对象转换为字符串,如从解析错误中可以看到的。
You should be calling nameEditText.getText().toString()
, which will get the input text from the object as a string. 您应该调用
nameEditText.getText().toString()
,它将以字符串形式从对象获取输入文本。
I' m guessing now but is it the case that when you saved this date value to the db from an EditText you did something like: 我现在正在猜测,但是当您将这个日期值从EditText保存到db时,情况是这样的:
edittext.toString()
instead of 代替
edittext.getText().toString()
If this is the case then make the change because instead of the text of the EditText you saved "android.support.v7.widget.AppCompatEditText{ceb966d VFED..CL. .F...
如果是这种情况,请进行更改,因为您保存的是
"android.support.v7.widget.AppCompatEditText{ceb966d VFED..CL. .F...
,而不是EditText的文本"android.support.v7.widget.AppCompatEditText{ceb966d VFED..CL. .F...
The error is in your code reading the edittext: 错误在于您的代码中读取了edittext:
Replace timeEditText.toString();
替换
timeEditText.toString();
with timeEditText.getText().toString();
与
timeEditText.getText().toString();
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.