I'm trying to sort an ArrayList
in Java in Android app but I'm getting this weird exception.
Code:
eventsList.sort(new Comparator<Event>() {
@Override
public int compare(Event event, Event t1) {
return event.getEventStartDate().compareTo(t1.getEventStartDate());
}
});
Exception:
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)
ArrayList#sort()
was added in API level 24 and runtimes below API level 24 don't have that method. Looks like your compileSdkVersion
is at 24 so you got the code to compile in the first place.
Use Collections.sort(list, comparator)
instead.
Zxing?
If you get this error in the Zxing core lib in com.google.zxing.qrcode.detector.FinderPatternFinder.selectBestPatterns
you can solve it by downgrading Zxing to 3.3.x (3.3.3 currently).
See https://github.com/zxing/zxing/issues/1170 for details.
What if you try
Collections.sort(eventsList, new Comparator...
As far as I know ArrayList
doesn't have sort
method.
List doesn't have its own sorting method, you'll need to call
Collections.sort()
as the method on the list. If this returns a ClassCastError, that means the list has non-sortable items. I think this should fix it, but without full code, it's hard to check.
In Zxing no need to downgrade version
just add following in App level gradle
android{
....
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
}
}
and
dependencies {
......
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}
ArrayList.sort() method was added later that's why its not working. We can update java version (1.8) or we can use below method.
Collections.sort(eventList, new Comparator<Event>() {
@Override
public int compare(Event event, Event t1) {
return event.getEventStartDate().compareTo(t1.getEventStartDate());
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.