简体   繁体   English

java.lang.NoClassDefFoundError:解析失败:Ljava/time/LocalDate; 错误

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

I tried everything, but the error not getting solved我尝试了一切,但错误没有得到解决

Here is my code:这是我的代码:

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;
}

I have written the code to get dates of one week.我已经编写了代码来获取一周的日期。 When I running this code, i am getting an error at LocalDate.now();.当我运行此代码时,我在 LocalDate.now(); 处收到错误消息。 The error is "java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalDate;".错误是“java.lang.NoClassDefFoundError:解析失败:Ljava/time/LocalDate;”。 I have searched on the net.我在网上搜索过。 Some are saying it requires API level min 27. I have tired on API 27 level but the error didn't get resolved.有人说它需要 API 级别至少 27。我已经厌倦了 API 27 级别,但错误没有得到解决。 More ever my target device is API 26 I needed to run on this API level.我的目标设备是 API 26,我需要在这个 API 级别上运行。 And also tried adding "compile "java.time:LocalTime:1.8" ".并且还尝试添加“编译”java.time:LocalTime:1.8“”。 This one also doesn't work.这个也行不通。 Please help me...请帮我...

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. NoClassDefFoundError - 如果 Java 虚拟机或 ClassLoader 实例尝试加载类的定义(作为正常方法调用的一部分或作为使用 new 表达式创建新实例的一部分)并且没有类的定义可以被抛出,则抛出成立。

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

Read LocalDate读取LocalDate

Make sure, You added this in your build.gradle section.确保,您在build.gradle部分添加了它。

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
    }

FYI供参考

java.time package was added only in API 26. java.time包仅在 API 26 中添加。

So, You should set所以,你应该设置

 minSdkVersion 26

After that, Clean-Rebuild-Gradle .之后, Clean-Rebuild-Gradle

You are trying to run your app on a device (or simulator) with an API level below 26. LocalDate and the rest of java.time were introduced in level 26. However, there is a backport that allows you to use the most used 80 % of it on earlier Android versions.您正在尝试运行的设备(或模拟器)低于26的API级别上应用LocalDate和java.time其余在26级但是进行了介绍,有一个补丁包,使您可以使用最常用80 % 在早期的 Android 版本上。 I suggest that this is a good solution for you.我建议这对你来说是一个很好的解决方案。

So add the ThreeTenABP library to your project.因此,将 ThreeTenABP 库添加到您的项目中。 And make sure you import org.threeten.bp.LocalDate and org.threeten.bp.DayOfWeek rather than the versions from java.time .并确保您导入org.threeten.bp.LocalDateorg.threeten.bp.DayOfWeek而不是从版本java.time Then you should be fine.那你应该没问题。

I was once told that dependencies is (I didn't test):我曾经被告知依赖项是(我没有测试):

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

TemporalAdjusters时间调节器

As an aside your code may be written as:顺便说一句,您的代码可以写为:

    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));

I have used the following imports:我使用了以下导入:

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

Links链接

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.如果 Java 虚拟机或 ClassLoader 实例尝试加载类的定义(作为正常方法调用的一部分或作为使用 new 表达式创建新实例的一部分)并且找不到类的定义,则抛出。 The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.编译当前正在执行的类时,搜索到的类定义存在,但无法再找到该定义。

Use SimpleDateFormat and Calendar.使用 SimpleDateFormat 和 Calendar。 You may also need different implementations for pre and post sdk 26您可能还需要针对 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());
        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NoClassDefFoundError:解析失败:Ljava/time/Instant; - java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/Instant; java.lang.NoClassDefFoundError:无法解决以下问题:Ljava / lang / invoke / MethodType; - java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/invoke/MethodType; java.lang.NoClassDefFoundError:解析失败 - java.lang.NoClassDefFoundError: Failed resolution 如何将StreamingReader与资产文件一起使用,获取java.lang.NoClassDefFoundError:无法解决以下问题:[Ljava / nio / file / attribute / FileAttribute; - how to use StreamingReader with asset files , getting java.lang.NoClassDefFoundError: Failed resolution of: [Ljava/nio/file/attribute/FileAttribute; 'java.lang.NoClassDefFoundError java.lang.ClassNotFoundException 导致的 Ljava/util/Base64 解析失败 未找到 class “java.util.Base64” - 'java.lang.NoClassDefFoundError Failed resolution of Ljava/util/Base64 caused by java.lang.ClassNotFoundException Didn't find class "java.util.Base64" java.lang.NoClassDefFoundError:解析失败:Ljavax / naming / directory / InitialDirContext; - java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/naming/directory/InitialDirContext; java.lang.NoClassDefFoundError:解析失败:Landroid / webkit / SafeBrowsingResponse - java.lang.NoClassDefFoundError: Failed resolution of: Landroid/webkit/SafeBrowsingResponse java.lang.noClassDefFoundError:无法解决以下问题:Lcom / facebook / R $ style; - java.lang.noClassDefFoundError: Failed resolution of: Lcom/facebook/R$style; Java.lang.NoClassDefFoundError:解析失败:Lcom/google/firebase/FirebaseApp - Java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/firebase/FirebaseApp java.lang.NoClassDefFoundError:解析失败:Landroid/graphics/BlendModeColorFilter; - java.lang.NoClassDefFoundError: Failed resolution of: Landroid/graphics/BlendModeColorFilter;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM