java – NoClassDefFoundError当我使用lambda来遍历String数组

前端之家收集整理的这篇文章主要介绍了java – NoClassDefFoundError当我使用lambda来遍历String数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我使用lambda来遍历String数组时,我遇到了错误.
java.lang.NoClassDefFoundError: com.twsz.app.ivyBox.alarm.CreateOrUpdateAlarmActivity$$Lambda$1
at com.twsz.app.ivyBox.alarm.CreateOrUpdateAlarmActivity.initView(CreateOrUpdateAlarmActivity.java:143)
at com.twsz.app.ivyBox.alarm.CreateOrUpdateAlarmActivity.onCreate(CreateOrUpdateAlarmActivity.java:73)

这是我的代码.我知道传统方法遍历String数组,但为什么当我使用lambda时会发生这种情况.

String[] days = dayOfWeek.split(",");
    Arrays.asList(days).forEach(day->{
        int index = Integer.valueOf(day) -1;
        checkBoxList.get(index).setChecked(true);
    });//where happens NoClassDefFoundError

我的build.gradle文件

android {
        compileSdkVersion 25
        buildToolsVersion "25.0.2"
        defaultConfig {
            applicationId "com.twsz.app.ivyBox"
            minSdkVersion 14
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            vectorDrawables.useSupportLibrary = true
        }

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }

        dataBinding {
            enabled = true
        }
    }

感谢任何帮助.

解决方法

*
     * @param action The action to be performed for each element
     * @throws NullPointerException if the specified action is null
     * @since 1.8
     */
    default void forEach(Consumer<? super T> action) {
        Objects.requireNonNull(action);
        for (T t : this) {
            action.accept(t);
        }
    }

forEach是默认方法,它只支持java8.

Android does not support all Java 8 language features. However,the following features are available when developing apps targeting Android 7.0 (API level 24):

Default and static interface methods
Lambda expressions (also available on API level 23 and lower)
Repeatable annotations
Method References (also available on API level 23 and lower)
Type Annotations (also available on API level 23 and lower)

Android支持默认和静态接口方法,但它需要API级别24.更多细节here

defaultConfig {
            applicationId "com.twsz.app.ivyBox"
            minSdkVersion 14 // Your minSdkVersion is less than 24
            targetSdkVersion 25
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            vectorDrawables.useSupportLibrary = true
        }

当您在系统中运行不到24的应用程序时,您将获得该异常.所以你最好改变另一种方式.传统循环或Rxjava2.

原文链接:https://www.f2er.com/java/125820.html

猜你在找的Java相关文章