Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSuchMethodError: No interface method getOrDefault(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; in class Ljava/util/Map;

I've got the crash on Meizu device with Android 5.0. This crash doesn't appear on most of the devices.

The error is: java.lang.NoSuchMethodError: No interface method getOrDefault(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; in class Ljava/util/Map; or its super classes (declaration of 'java.util.Map' appears in /system/framework/core-libart.jar)

enter image description here

Did someone experience something similar?

like image 693
Bohdan Samusko Avatar asked Oct 16 '19 08:10

Bohdan Samusko


2 Answers

The getOrDefault method was added in API level 24 and runtimes below API level 24 don't have that method. Thats why it's not working in Android API level 21.

like image 174
Md. Asaduzzaman Avatar answered Oct 09 '22 23:10

Md. Asaduzzaman


Especially this function you can do this:

//ask your app running more modern API as level 24 (Build.VERSION_CODES.N(ougat))
result = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) ?          
    map.getOrDefault(value, default) :
    // if not, then need to solve with similar code of original code in next below
    ((map.get(value) != null) ? map.get(value) : default);    

There is a source code in the original one c:\Users\Your_username\AppData\Local\Android\Sdk\sources\android-29\java\util\Map.java:

    ...
    default V getOrDefault(Object key, V defaultValue) {
        V v;
        return (((v = get(key)) != null) || containsKey(key))
            ? v
            : defaultValue;
    }
    ...
like image 23
csaszizoltan Avatar answered Oct 09 '22 21:10

csaszizoltan