Search code examples
javaandroidnosuchmethoderrorandroid-compatibility

NoSuchMethod Error in ContentValues.keySet() on old android devices


I get a weird error when using this function. On new devices and emulator works well but when testing the app on old devices it crashes.. Any clue on what is happening?

   public boolean bind(ContentValues values ) throws DatabaseException {
        if (values == null) throw new DatabaseException("Values is null");

        ArrayList<String> columnNames = this.getColumnNamesArray(AppController.getDBO().getDatabase());
        String currentKey = null;

        try {
            for (String key : values.keySet()) {
                currentKey = key;
                if (columnNames.contains(key)) {
                    if (values.get(key ) == null ) {
                        this._properties.putNull(key);
                    } else if (values.get(key) instanceof String) {
                        this._properties.put(key, values.getAsString(key));
                    } else if (values.get(key) instanceof Integer) {
                        this._properties.put(key, values.getAsInteger(key));
                    } else if (values.get(key) instanceof Boolean) {
                        this._properties.put(key, values.getAsBoolean(key));
                    } else if (values.get(key) instanceof Byte) {
                        this._properties.put(key, values.getAsByte(key));
                    } else if (values.get(key) instanceof Double) {
                        this._properties.put(key, values.getAsDouble(key));
                    } else if (values.get(key) instanceof Float) {
                        this._properties.put(key, values.getAsFloat(key));
                    } else if (values.get(key) instanceof Long) {
                        this._properties.put(key, values.getAsLong(key));
                    } else if (values.get(key) instanceof Short) {
                        this._properties.put(key, values.getAsShort(key));
                    }
                }
            }
        } catch (Exception e) {
            Log.e(AppController.DEBUG_TAG, "Exception raised: " + getClass().getSimpleName(), e);
            throw new DatabaseException(e.toString(),currentKey);
        } catch (NoSuchMethodError error) {
            // Raised in old devices:
            Log.wtf(AppController.ERROR_TAG, error.getMessage());
            return false;
        }

        return true;

    }

The error i get is:

E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NoSuchMethodError: keySet

Thanks in advance!


Solution

  • NoSuchMethod Error in ContentValues.keySet() on old android devices

    This means that you need to use different (workaround) solution for API < 11 since keyset() is available from API 11. What about to use valueSet() that is available from API 1?

    // solution for API < 11
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
       for (Entry<String, Object> item : cv.valueSet()) {
          String key = item.getKey(); // getting key
          Object value = item.getValue(); // getting value
          ...
          // do your stuff
       }
    }
    
    // solution for API >= 11
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
       // your current solution
    }