在caling,stream()方法时收集NoSuchMethodError

时间:2016-04-15 16:43:57

标签: java android libgdx

  public int getColsBy(final Collection<Room> rooms) {
    return rooms.stream()
        .max((lhs, rhs) -> lhs.right - rhs.right).get().right;
  }

我尝试在我的libgdx项目中执行此代码,但我所有的都是例外!

04-15 22:34:21.136 32610-32636/com.mygdx.game E/AndroidRuntime: FATAL EXCEPTION: GLThread 8116
                                                            Process: com.mygdx.game, PID: 32610
                                                            java.lang.NoSuchMethodError: No interface method stream()Ljava/util/stream/Stream; in class Ljava/util/Collection; or its super classes (declaration of 'java.util.Collection' appears in /system/framework/core-libart.jar)
                                                                at com.mygdx.game.enviroment.LevelParser.getColsBy(LevelParser.java:44)
                                                                at com.mygdx.game.enviroment.LevelParser.parseFrom(LevelParser.java:30)
                                                                at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:38)
                                                                at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:243)
                                                                at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1550)
                                                                at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1272)

有我的build.gradle文件

   buildscript {
    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        jcenter()
    }
    dependencies {
        classpath 'de.richsource.gradle.plugins:gwt-gradle-plugin:0.6'
        classpath 'com.android.tools.build:gradle:2.1.0-beta1'
        classpath 'org.robovm:robovm-gradle-plugin:1.12.0'
        classpath 'me.tatarka:gradle-retrolambda:3.3.0-beta4'
        classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
        configurations.classpath.exclude group: 'com.android.tools.external.lombok'
    }
}

allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"

    version = '1.0'
    ext {
        appName = "my-gdx-game"
        gdxVersion = '1.7.2'
        roboVMVersion = '1.12.0'
        box2DLightsVersion = '1.4'
        ashleyVersion = '1.7.0'
        aiVersion = '1.7.0'
    }

    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
    }
}

project(":desktop") {
    apply plugin: "java"


    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
    }
}

project(":android") {
    apply plugin: "android"
    apply plugin: "com.android.application"
    apply plugin: "me.tatarka.retrolambda"

    configurations { natives }

    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
    }

}

project(":ios") {
    apply plugin: "java"
    apply plugin: "robovm"


    dependencies {
        compile project(":core")
        compile "org.robovm:robovm-rt:$roboVMVersion"
        compile "org.robovm:robovm-cocoatouch:$roboVMVersion"
        compile "com.badlogicgames.gdx:gdx-backend-robovm:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-ios"
    }
}

project(":html") {
    apply plugin: "gwt"
    apply plugin: "war"


    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx:$gdxVersion:sources"
        compile "com.badlogicgames.gdx:gdx-backend-gwt:$gdxVersion:sources"
    }
}

project(":core") {
    apply plugin: "java"
    apply plugin: "me.tatarka.retrolambda"

    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
    }
}

tasks.eclipse.doLast {
    delete ".project"
}

在项目中使用流程应该怎么做?

3 个答案:

答案 0 :(得分:4)

您可以执行某些操作。 Android确实从版本7(Nougat)支持Java 8。

使用(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)

围绕您的溪流api通话
public int getColsBy(final Collection<Room> rooms) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
      return rooms.stream().max((lhs, rhs) -> lhs.right - rhs.right).get().right;
}else{
      return "max the old fashion way"
     }
}

我不喜欢外部库,这就是我的工作。

答案 1 :(得分:1)

你无能为力。 Android不支持Java 8(Jack&amp; Jill将部分支持Java 8)。

使用第三方插件retrolambda仅支持lambda。

您可以尝试RxJava https://github.com/ReactiveX/RxJava

而不是Java 8

答案 2 :(得分:0)

你应该用 Build.VERSION.SDK_INT >= Build.VERSION_CODES.N

包围你的流 api 调用

我将使用一个示例,该示例采用字符串 id 和字符串的多个占位符。

// Return string with placeholders
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

    return context.getResources().getString(stringId, Arrays.stream((newArgs)).toArray());

} else {

    //noinspection ConfusingArgumentToVarargsMethod
    return context.getResources().getString(
                    stringId,
                    Arrays.copyOf(newArgs,
                            newArgs.length,
                            String[].class)
    );
}

我的完整示例函数如下

/**
  * Function to get string resource with multiple integer placeholders
  *
  * @param context  - for getting resources
  * @param stringId - string resource id
  * @param args     - placeholders ids
 */
 @RequiresApi(api = Build.VERSION_CODES.N)
 public static String getStringResource(@NonNull Context context, int stringId, @NonNull Integer... args) {

    // Create new Object array with same size as args length
    Object[] newArgs = new Object[args.length];

    // Loop through passed object with string id
    for (int i = 0; i < args.length; i++) {

        // Get string from string id adding them to new Object array
        newArgs[i] = context.getResources().getString(args[i]);
    }

    // Return string with placeholders
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

        return context.getResources().getString(stringId, Arrays.stream((newArgs)).toArray());

    } else {

        //noinspection ConfusingArgumentToVarargsMethod
        return context.getResources().getString(
                stringId,
                Arrays.copyOf(newArgs,
                        newArgs.length,
                        String[].class
                )
        );
    }
}