java.lang.NoSuchMethodError:排序arraylist android时没有接口方法sort(Ljava/util/Comparator;)异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38951218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NoSuchMethodError: No interface method sort(Ljava/util/Comparator;) exception in sorting arraylist android
提问by Hossam Ghareeb
I'm trying to sort an ArrayList
in Java in Android app but I'm getting this weird exception.
我正在尝试ArrayList
在 Android 应用程序中对 Java 中的an 进行排序,但我遇到了这个奇怪的异常。
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)
回答by laalto
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.
ArrayList#sort()
在 API 级别 24 中添加并且低于 API 级别 24 的运行时没有该方法。看起来您compileSdkVersion
是 24岁,所以您首先获得了要编译的代码。
Use Collections.sort(list, comparator)
instead.
使用Collections.sort(list, comparator)
来代替。
回答by tobltobs
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).
如果您在 Zxing 核心库中遇到此错误,com.google.zxing.qrcode.detector.FinderPatternFinder.selectBestPatterns
您可以通过将 Zxing 降级到 3.3.x(当前为 3.3.3)来解决它。
See https://github.com/zxing/zxing/issues/1170for details.
有关详细信息,请参阅https://github.com/zxing/zxing/issues/1170。
回答by mic4ael
What if you try
如果你尝试怎么办
Collections.sort(eventsList, new Comparator...
As far as I know ArrayList
doesn't have sort
method.
据我所知ArrayList
没有sort
方法。
回答by Matt
List doesn't have its own sorting method, you'll need to call
List 没有自己的排序方法,你需要调用
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.
作为列表中的方法。如果这返回 ClassCastError,则意味着列表具有不可排序的项目。我认为这应该解决它,但没有完整的代码,很难检查。
回答by Ravindra
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.
ArrayList.sort() 方法是后来添加的,这就是它不起作用的原因。我们可以更新 java 版本 (1.8) 或者我们可以使用下面的方法。
Collections.sort(eventList, new Comparator<Event>() {
@Override
public int compare(Event event, Event t1) {
return event.getEventStartDate().compareTo(t1.getEventStartDate());
}
});