Java – NoSuchMethodError – No static method comparison (java/util/Comparator)

NoSuchMethodError – No static method comparison (java/util/Comparator)… here is a solution to the problem.

NoSuchMethodError – No static method comparison (java/util/Comparator)

I’m using Gluon on my Android device to sort List from java.util. The app doesn’t compile for iOS even with 8GB allocated (out of memory), but that’s another issue.

classpath 'org.javafxports:jfxmobile-plugin:1.3.4'

Compile 'com.gluonhq:charm:4.3.2'

jfxmobile version = '3.2.4'

This line that caused the crash:

highscoreList.sort(comparison(Highscore::getScore).reversed());//#89

03-22 09:42:14.709 27312 27337 E AndroidRuntime: FATAL EXCEPTION: JavaFX Application Thread
03-22 09:42:14.709 27312 27337 E AndroidRuntime: Process: com.x.pacman, PID: 27312
03-22 09:42:14.709 27312 27337 E AndroidRuntime: java.lang.NoSuchMethodError: No static method comparing(Ljava/util/function/Function;)Ljava/util/Comparator; in class Ljava/util/Comparator; or its super classes (declaration of 'java.util.Comparator' appears in /system/framework/core-libart.jar)
03-22 09:42:14.709 27312 27337 E AndroidRuntime:        at com.x.pacman.HighscoreUtil.readHighscoreList(HighscoreUtil.java:89)

I started searching and found this post about NoSuchMethodError , so I tried doing the following but it still crashes and now I have no idea :

Comparator<Highscore> comparator = new Comparator<Highscore>() {
    @Override
    public int compare(Highscore highscore1, Highscore highscore2) {
        return highscore1.getScore() - highscore2.getScore();
    }
};

highscoreList.sort(comparator); #97

03-22 11:30:48.836 16547 16570 E AndroidRuntime: java.lang.NoSuchMethodError: No interface method sort(Ljava/util/Comparator;)V in class Ljava/util/List; or its super classes (declaration of 'java.util.List' appears in /system/framework/core-libart.jar)
03-22 11:30:48.836 16547 16570 E AndroidRuntime:        at com.kaufland.pacman.HighscoreUtil.readHighscoreList(HighscoreUtil.java:97)

Solution

Both are Comparator#comparing()>List #sort (Comparator) is added at API level 24, your device may be running a lower API level.

Use Collections#sort() Might be better since API level 1.

Related Problems and Solutions