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

Viewed 3129

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?

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.

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;
    }
    ...
Related