当我使用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.