使用此功能时出现奇怪的错误。在新设备和模拟器上可以很好地运行,但是在旧设备上测试应用程序时会崩溃。有关发生了什么的任何线索?

   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;

    }


我得到的错误是:


  E / AndroidRuntime:致命异常:主要
      java.lang.NoSuchMethodError:keySet


提前致谢!

最佳答案

旧Android设备上ContentValues.keySet()中的NoSuchMethod错误


这意味着您需要为API keyset()。如何使用API​​ 1提供的valueSet()

// 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
}

10-07 18:18