java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalDate; error

java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalDate; error

我尝试了所有方法,但错误没有得到解决

这是我的代码:

public String[] getWeekDays() {

    LocalDate today = LocalDate.now();
    LocalDate monday = today;
    LocalDate tue = today;
    LocalDate wed = today;
    LocalDate thur = today;
    LocalDate fri = today;
    LocalDate sat = today;
    LocalDate sunday = today;
    while (sunday.getDayOfWeek() != DayOfWeek.SUNDAY){
        sunday = sunday.minusDays(1);
     }
     while (monday.getDayOfWeek() != DayOfWeek.MONDAY){
        monday = monday.minusDays(1);
     }
     while (tue.getDayOfWeek() != DayOfWeek.TUESDAY){
        tue = tue.plusDays(1);
     }
     while (wed.getDayOfWeek() != DayOfWeek.WEDNESDAY){
        wed = wed.plusDays(1);
     }
     while (thur.getDayOfWeek() != DayOfWeek.THURSDAY){
        thur = thur.plusDays(1);
     }
     while (fri.getDayOfWeek() != DayOfWeek.FRIDAY){
        fri = fri.plusDays(1);
     }
     while (sat.getDayOfWeek() != DayOfWeek.SATURDAY){
        sat = sat.plusDays(1);
     }

     String[] days = {String.valueOf(sunday),String.valueOf(monday), String.valueOf(tue), String.valueOf(wed),
                String.valueOf(thur), String.valueOf(fri), String.valueOf(sat)};
    return days;
}

我已经编写了获取一周日期的代码。当我 运行ning 这段代码时,我在 LocalDate.now(); 处收到错误。错误是 "java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalDate;"。 我在网上搜索过。有人说它需要 API 至少 27 级。我已经厌倦了 API 27 级,但错误没有得到解决。更何况我的目标设备是 API 26 我需要在这个 API 级别上 运行。并且还尝试添加 "compile "java.time:LocalTime:1.8" "。这个也行不通。 请帮助我...

NoClassDefFoundError-Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

LocalDate today = LocalDate.now(); // Added 1.8

阅读LocalDate

请确保,您在 build.gradle 部分添加了此内容。

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
    }

仅供参考

java.time 包仅在 API 26.

中添加

所以,你应该设置

 minSdkVersion 26

之后Clean-Rebuild-Gradle.

您正在尝试在 API 级别低于 26 的设备(或模拟器)上 运行 您的应用程序。 LocalDate 和 java.time 的其余部分已引入在第 26 级。但是,有一个向后移植允许您在早期 Android 版本上使用最常用的 80%。我建议这对你来说是一个很好的解决方案。

因此将 ThreeTenABP 库添加到您的项目中。并确保导入 org.threeten.bp.LocalDateorg.threeten.bp.DayOfWeek 而不是 java.time 中的版本。那你应该没问题。

我曾经被告知依赖项是(我没有测试):

compile group: 'org.threeten', name: 'threetenbp', version: '1.3.3', classifier: 'no-tzdb'

时间调整器

顺便说一句,您的代码可以写成:

    LocalDate today = LocalDate.now(ZoneId.of("America/Tortola"));
    LocalDate monday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
    LocalDate tue = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.TUESDAY));
    LocalDate wed = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY));
    LocalDate thur = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.THURSDAY));
    LocalDate fri = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
    LocalDate sat = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY));
    LocalDate sunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

我使用了以下导入:

import org.threeten.bp.DayOfWeek;
import org.threeten.bp.LocalDate;
import org.threeten.bp.ZoneId;
import org.threeten.bp.temporal.TemporalAdjusters;

链接

使用 SimpleDateFormat 和日历。您可能还需要 pre 和 post sdk 26

的不同实现
    private String getCurrentDateTime() {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            Log.d(TAG, "getCurrentDateTime: greater than O");
            return LocalDateTime.now().format(DateTimeFormatter.ofPattern("h:m a"));
        } else{
            Log.d(TAG, "getCurrentDateTime: less than O");
            SimpleDateFormat SDFormat = new SimpleDateFormat("h:m a");
            return SDFormat.format(new Date());
        }
}

使用Java8API脱糖Google官方支持:https://developer.android.com/studio/write/java8-support

它使您能够在旧平台上使用许多 Java 8 API。