NoClassDefFoundError when i use lambda to traverse String array

Cyrus :

I got follow error when I use lambda to traverse String array.

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)

This is my code.I know it's ok for traditional way to traverse the String array ,but why this happen when I use lambda.

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

My build.gradle file

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

Thanks for any help.

Mr.ccc :
     *
     * @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 is default method , it’s only supported by 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 support default and static interface methods , but it needs API level 24.More details 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
        }

When you run your app in system less than 24, you will get that exception. so you’d better change another way . Traditional loop or Rxjava2.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=432365&siteId=1