当前位置:网站首页>Record a murder case caused by ignoring the @suppresslint ("newapi") prompt

Record a murder case caused by ignoring the @suppresslint ("newapi") prompt

2022-06-11 07:55:00 tinyvampirepudge

Remember to ignore once @SuppressLint(“NewApi”) Prompt the blood case caused

This development uses Java Priority queue in , You need to sort the elements according to the given priority , So I wrote the following code :

private Comparator<CustomObject> comparator = new Comparator<CustomObject>() {
    @Override
    public int compare(CustomObject o1, CustomObject o2) {
        /**
         *  negative 、0、 Positive numbers 
         *  Less than 、 be equal to 、 Greater than 
         */
        return o1.priority() - o2.priority();
    }
};


/**
 *  Priority queue 
 */
private PriorityQueue<CustomObject> queue = new PriorityQueue<>(comparator);

What's different is that , stay new PriorityQueue<>(comparator) The constructor code has red at the bottom error Tips ,IDE Give the prompt of version compatibility , Here's the picture :

 Insert picture description here

Yes, of course , As a mature Developer , I have no hesitation 、 Press the button without hesitation option + enter key , Used a way to hide the problem ,SuppressLint annotation , The code is as follows :

    @SuppressLint("NewApi")
    private PriorityQueue<CustomObject> queue = new PriorityQueue<>(comparator);

The annoying hint finally disappeared , It's much fresher .

I'll pay you back sooner or later , It wasn't long before I received crash The report , The details are as follows :

java.lang.NoSuchMethodError: No direct method (Ljava/util/Comparator;)V in class Ljava/util/PriorityQueue; or its super classes (declaration of 'java.util.PriorityQueue' appears in /system/framework/core-libart.jar)

At the end of the day , I used a version that did not exist in the lower version api Method ,IDE Gave me a hint to change , Then I found the prompt annoying , Just use annotations to hide the hints , The problem is still unsolved .

ok , Solve the problem seriously . After consideration , take PriorityQueue + Comparator Replace with List + Comparator + Collections.sort(list, comparator) The way , Problem solved .

This story tells us ,IDE I won't give you any hints if I have nothing to do , Let's have some snacks later .

Reference resources :
https://stackoverflow.com/questions/36103606/nosuchmethoderror-for-comparator/36103681

原网站

版权声明
本文为[tinyvampirepudge]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110747210703.html