摩托罗拉设备:在ThreeTen中解析日期时出现org.threeten.bp.DateTimeException [英] Motorola devices : org.threeten.bp.DateTimeException when parsing a date in ThreeTen

查看:190
本文介绍了摩托罗拉设备:在ThreeTen中解析日期时出现org.threeten.bp.DateTimeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些LocalDateTime.now()返回0000-00-00T00:00:00.0 ThreeTenABP .

代码如下:

@Override
protected void onResume() {
    super.onResume();
    if (!TextUtils.isEmpty(timeout)) {
        LocalDateTime savedTime = LocalDateTime.parse(timeout, DateTimeFormatter.ISO_DATE_TIME);
        if (LocalDateTime.now().isAfter(savedTime)) {
            refresh()
        }
    }
}

@Override
protected void onPause() {
    super.onPause();
    LocalDateTime currentTime = LocalDateTime.now().plus(Duration.ofMinutes(10));
    timeout = currentTime.format(DateTimeFormatter.ISO_DATE_TIME);
}

仅在以下设备上(仅3台运行6.0的Motorola设备):

Only on these devices (only 3 Motorola devices running 6.0):

我有此崩溃:

Fatal Exception: java.lang.RuntimeException: Unable to resume activity {com.myapp/com.myapp.MainActivity}: org.threeten.bp.format.DateTimeParseException: Text '0000-00-00T00:00:00.8' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 0
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3121)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5443)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by org.threeten.bp.format.DateTimeParseException: Text '0000-00-00T00:00:00.8' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 0
       at org.threeten.bp.format.DateTimeFormatter.createError(DateTimeFormatter.java:1559)
       at org.threeten.bp.format.DateTimeFormatter.parse(DateTimeFormatter.java:1496)
       at org.threeten.bp.LocalDateTime.parse(LocalDateTime.java:444)
       at com.myapp.MainActivity.onResume(MainActivity.java:273)
       at android.app.Activity.performResume(Activity.java:6344)
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3110)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5443)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by org.threeten.bp.DateTimeException: Invalid value for MonthOfYear (valid values 1 - 12): 0
       at org.threeten.bp.temporal.ValueRange.checkValidValue(ValueRange.java:278)
       at org.threeten.bp.temporal.ChronoField.checkValidValue(ChronoField.java:557)
       at org.threeten.bp.LocalDate.of(LocalDate.java:237)
       at org.threeten.bp.chrono.IsoChronology.resolveDate(IsoChronology.java:452)
       at org.threeten.bp.format.DateTimeBuilder.mergeDate(DateTimeBuilder.java:297)
       at org.threeten.bp.format.DateTimeBuilder.resolve(DateTimeBuilder.java:206)
       at org.threeten.bp.format.DateTimeFormatter.parse(DateTimeFormatter.java:1491)
       at org.threeten.bp.LocalDateTime.parse(LocalDateTime.java:444)
       at com.myapp.MainActivity.onPostResume(MainActivity.java:273)
       at android.app.Activity.performResume(Activity.java:6344)
       at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3110)
       at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5443)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

第273行是:

LocalDateTime savedTime = LocalDateTime.parse(timeout, DateTimeFormatter.ISO_DATE_TIME);

因此,基本上LocaleDateTime.now()返回的是无效的日期时间,并且解析失败.

So basically LocaleDateTime.now() is returning an invalid date time and parsing it fails.

另一个有趣的事情是它仅在一月初以来发生过.有人遇到过这个问题吗?

The other interesting thing is that it only happened since beginning of January. Anyone has ever faced that problem?

推荐答案

这是旧的TBP上的一个已知错误:

This is a known bug on old TBP: Threetenbp - Date formatting fails on some Android devices But it is resolved on ThreeTenABP that you're using.

这三行几乎可以告诉大家:

These three lines pretty much tell you all:

1.

Fatal Exception: java.lang.RuntimeException: Unable to resume activity {com.myapp/com.myapp.MainActivity}: org.threeten.bp.format.DateTimeParseException: Text '0000-00-00T00:00:00.8' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 0

2.

Caused by org.threeten.bp.format.DateTimeParseException: Text '0000-00-00T00:00:00.8' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 0

3.

Caused by org.threeten.bp.DateTimeException: Invalid value for MonthOfYear (valid values 1 - 12): 0

您已经传递了"0000-00-00T00:00:00.8"作为参数.

You've passed '0000-00-00T00:00:00.8' as the argument.

我已经通过使用ZonedDateTime(也建议使用它是最安全的方法,甚至由Google推荐)来解决此问题,而不是使用LocalDateTime.

I've solved this by using ZonedDateTime (also advised to be the safest one to use, and recommended by even Google) instead of LocalDateTime.

在ThreeTenABP上执行任何操作之前,还有一个愚蠢的问题. 您是否在Application类中初始化ThreeTenABP?

And one stupid question before you do anything on ThreeTenABP. Did you init ThreeTenABP in your Application class?

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        AndroidThreeTen.init(this); // Without this ThreeTenABP cannot work properly
    }
}

此外,请勿使用任何其他日期/时间库,因为它们都已弃用,目前仅支持的两个是ThreeTenABP(如果您需要在API 26之前的设备上运行应用程序)和java.time( API 26+),别无其他.不要谈论其他旧库的性能问题.

这篇关于摩托罗拉设备:在ThreeTen中解析日期时出现org.threeten.bp.DateTimeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆