Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoClassDefFoundError when i use lambda to traverse String array

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.

like image 392
Cyrus Avatar asked May 04 '17 06:05

Cyrus


2 Answers

     *
     * @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.

like image 137
Mr.ccc Avatar answered Sep 21 '22 19:09

Mr.ccc


forEach method is available only in Android N, you can't use any API from Java 8 before SDK 24.

Even if you are using Retrolambda or Jack.

You need to use regular for loop.

like image 43
Dimezis Avatar answered Sep 20 '22 19:09

Dimezis