简体   繁体   English

调用时收集 NoSuchMethodError ,stream() 方法

[英]Collection NoSuchMethodError when caling ,stream() method

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

I try to execute this code in my libgdx project, but all i have is exception!我尝试在我的 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)

There is my build.gradle file有我的 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"
}

What things should I do to use streams in my projects?我应该怎么做才能在我的项目中使用流?

There is something you can do .你可以做些什么 Android does support Java 8 from version 7 (Nougat). Android 从版本 7 (Nougat) 开始支持 Java 8。

Surround your streams api call with (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)使用(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"
     }
}

I don't like external libraries, and this is what I do.我不喜欢外部库,这就是我所做的。

There is nothing you can do.你无能为力。 Android is not supporting Java 8 (Jack&Jill is going to support Java 8 partially). Android 不支持 Java 8(Jack&Jill 将部分支持 Java 8)。

Only lambdas are supported by using third party plugin retrolambda .使用第三方插件retrolambda仅支持 lambda。

Instead of Java 8 you can try RxJava https://github.com/ReactiveX/RxJava您可以尝试RxJava而不是 Java 8 https://github.com/ReactiveX/RxJava

You should Surround your streams api call with Build.VERSION.SDK_INT >= Build.VERSION_CODES.N您应该使用Build.VERSION.SDK_INT >= Build.VERSION_CODES.N环绕您的流 api 调用

I will use an example that takes a string id and multiple placeholder for the string.我将使用一个示例,该示例采用字符串 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)
    );
}

My full example function is as below我的完整示例功能如下

/**
  * 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
                )
        );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM