java.lang.NoSuchMethodError: No interface method sort(Ljava/util/Comparator;) exception in sorting arraylist android

前端 未结 5 1257
青春惊慌失措
青春惊慌失措 2020-12-09 16:16

I\'m trying to sort an ArrayList in Java in Android app but I\'m getting this weird exception.

Code:

eventsList.sort(new Comparator<         


        
相关标签:
5条回答
  • 2020-12-09 16:19

    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.

    0 讨论(0)
  • 2020-12-09 16:27

    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.

    0 讨论(0)
  • 2020-12-09 16:28

    What if you try

    Collections.sort(eventsList, new Comparator...
    

    As far as I know ArrayList doesn't have sort method.

    0 讨论(0)
  • 2020-12-09 16:30

    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.

    0 讨论(0)
  • 2020-12-09 16:40

    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());
            }
    });
    
    0 讨论(0)
提交回复
热议问题