Android上的ThreeTen-Backport错误 - ZoneRulesException:未注册任何时区数据文件 [英] ThreeTen-Backport error on Android - ZoneRulesException: No time-zone data files registered

查看:881
本文介绍了Android上的ThreeTen-Backport错误 - ZoneRulesException:未注册任何时区数据文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的Android项目使用ThreeTen-Backport库(因为java.time尚未在android开发中实现)。

I'm using ThreeTen-Backport library for my Android project (because java.time is not yet implemented in android development).

当我今天写 LocalDate = LocalDate.now(); LocalTime time = LocalTime.now(); 我收到以下异常:

When I write LocalDate today=LocalDate.now(); or LocalTime time=LocalTime.now(); I get the following exception:

Caused by: org.threeten.bp.zone.ZoneRulesException: 
  No time-zone data files registered   
      at org.threeten.bp.zone.ZoneRulesProvider.getProvider(ZoneRulesProvider.java:176)
      at org.threeten.bp.zone.ZoneRulesProvider.getRules(ZoneRulesProvider.java:133)
      at org.threeten.bp.ZoneRegion.ofId(ZoneRegion.java:143)
      at org.threeten.bp.ZoneId.of(ZoneId.java:357)
      at org.threeten.bp.ZoneId.of(ZoneId.java:285)
      at org.threeten.bp.ZoneId.systemDefault(ZoneId.java:244)
      at org.threeten.bp.Clock.systemDefaultZone(Clock.java:137)
      at org.threeten.bp.LocalDate.now(LocalDate.java:165)

同样的代码行在我拥有的另一个java项目中运行良好,该项目使用本机java.time库。

The same line of code works well in another java project I have, which uses the native java.time library.

我搜索了一个可能的解决方案,但找不到任何有用的东西:一个解决方案建议我需要使用另一个包含时区规则的jar,其他人建议那里可能是类路径中的两个或更多ThreeTenBP库。

这些情况与我的情况不符。

I searched for a possible solution but couldn't find anything useful: one solution suggested I need to use another jar that includes the time-zone rules and other suggested that there might be two or more ThreeTenBP-libraries inside the classpath.
Those cases don't match my case.

build.gradle 文件,在依赖项部分,我尝试了一些配置:

Inside the build.gradle file, at the dependencies section, I've tried few configurations:


  • 首先,我用 - 编译'com.jakewharton.threetenabp:threetenabp:1.0.3'

  • 然后,我试过 -
    编译'org.threeten:threetenbp:1.0.3'

  • 之后,我尝试了 -
    编译'org.threeten:threetenbp:1.3.1'

  • 目前,我使用 compile'org.threeten:threetenbp: 1.3.2'

  • At first, I used - compile 'com.jakewharton.threetenabp:threetenabp:1.0.3'
  • Then, I tried - compile 'org.threeten:threetenbp:1.0.3'
  • After that, I tried - compile 'org.threeten:threetenbp:1.3.1'
  • Currently, I use compile 'org.threeten:threetenbp:1.3.2'

我不知道该行代码有什么问题以及如何修复它。

LocalDate.now() LocalTime。 now()方法应该可以在不指定时区的情况下工作。

I don't know what is wrong with that line of code and how to fix it.
The LocalDate.now() and LocalTime.now() methods should work without specifying a time zone.

推荐答案

对于Android项目,你应该使用

For Android project you should use

compile 'com.jakewharton.threetenabp:threetenabp:1.0.3

确保在使用库中的类之前调用​​ AndroidThreeTen.init(this); 。这将读取时区数据(包含在库中)。您可以在 onCreate 方法中的应用程序类中初始化库,就像在README

Make sure you call AndroidThreeTen.init(this); before using the classes from the library. This will read the time zones data (included in the library). You can initialize the library in your Application class in the onCreate method just like it is recommended in the README.

这篇关于Android上的ThreeTen-Backport错误 - ZoneRulesException:未注册任何时区数据文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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