{"record":{"id":"ce467b3088db75dd","repo":"MuntashirAkon/AppManager","slug":"invalid-value-for-key-name-value-value","errorCode":null,"errorMessage":"Invalid value for key: ${name} (value: ${value})","messagePattern":"Invalid value for key: (.+?) \\(value: (.+?)\\)","errorType":"validation","errorClass":"java.io.IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/io/github/muntashirakon/AppManager/sharedpref/SharedPrefsUtil.java","lineNumber":147,"sourceCode":"                xmlSerializer.attribute(\"\", \"value\", value.toString());\n                xmlSerializer.endTag(\"\", TAG_LONG);\n            } else if (value instanceof String) {\n                xmlSerializer.startTag(\"\", TAG_STRING);\n                xmlSerializer.attribute(\"\", \"name\", name);\n                xmlSerializer.text(value.toString());\n                xmlSerializer.endTag(\"\", TAG_STRING);\n            } else if (value instanceof Set) {\n                xmlSerializer.startTag(\"\", TAG_SET);\n                xmlSerializer.attribute(\"\", \"name\", name);\n                //noinspection unchecked\n                for (String v : (Set<String>) value) {\n                    xmlSerializer.startTag(\"\", TAG_STRING);\n                    xmlSerializer.text(v);\n                    xmlSerializer.endTag(\"\", TAG_STRING);\n                }\n                xmlSerializer.endTag(\"\", TAG_SET);\n            } else {\n                throw new IOException(\"Invalid value for key: \" + name + \" (value: \" + value + \")\");\n            }\n        }\n        xmlSerializer.endTag(\"\", TAG_ROOT);\n        xmlSerializer.endDocument();\n        xmlSerializer.flush();\n        os.write(stringWriter.toString().getBytes());\n    }\n\n    @NonNull\n    public static String flattenToString(@NonNull Set<String> stringSet) {\n        List<String> stringList = new ArrayList<>(stringSet.size());\n        for (String string : stringSet) {\n            stringList.add(string.replace(\",\", \"\\\\,\"));\n        }\n        return TextUtils.join(\",\", stringList);\n    }\n\n    @NonNull","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/MuntashirAkon/AppManager/blob/0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5/app/src/main/java/io/github/muntashirakon/AppManager/sharedpref/SharedPrefsUtil.java#L129-L165","documentation":"IOException thrown by SharedPrefsUtil.writeSharedPref when the value passed for a preference key is not one of the serializable types the writer supports (String, Set<String>, etc.). The Android SharedPreferences XML format is type-tagged, so an unsupported value type cannot be represented and serialization of the whole file is aborted to avoid producing a corrupt XML.","triggerScenarios":"Calling writeSharedPref (or the higher-level setPreference API) with a Map containing a key whose value is an Integer, Boolean, Long, Float, byte[], or any other non-String/non-Set object; the if/else chain over value types exhausts and hits the final else throw.","commonSituations":"Importing preferences from JSON where numbers stay parsed as Integer instead of String; programmatically constructing the prefs map and forgetting to stringify values; version drift where a new pref type was added upstream without extending the writer.","solutions":["Convert all values to String (or Set<String>) before passing the map to writeSharedPref","Extend the writer with branches for the additional types you need (int, boolean, long, float)","Validate the map types before calling and reject/log unsupported entries early","Wrap the call in try-catch for IOException and report which key failed using the message's key name"],"exampleFix":"// before\nMap<String, Object> prefs = new HashMap<>();\nprefs.put(\"retry_count\", 3);\nwriteSharedPref(os, prefs);\n// after\nMap<String, Object> prefs = new HashMap<>();\nprefs.put(\"retry_count\", String.valueOf(3));\nwriteSharedPref(os, prefs);","handlingStrategy":"validation","validationCode":"for (Map.Entry<String, Object> e : prefs.entrySet()) {\n    Object v = e.getValue();\n    if (!(v instanceof String) && !(v instanceof Set)) {\n        throw new IllegalArgumentException(\"Unsupported value for \" + e.getKey());\n    }\n}","typeGuard":"static boolean isSerializablePref(Object v) {\n    return v instanceof String || (v instanceof Set<?> s && s.stream().allMatch(String.class::isInstance));\n}","tryCatchPattern":"try {\n    SharedPrefsUtil.writeSharedPref(os, prefs);\n} catch (IOException e) {\n    Log.e(TAG, \"Pref write failed: \" + e.getMessage(), e); // message names the offending key\n}","preventionTips":["Parse imported JSON with string values, not typed numbers","Keep one serialization path and add unit tests covering all pref types","Validate map value types at API boundaries","Default-convert unknown types via String.valueOf() instead of failing"],"tags":["io","serialization","sharedpreferences"],"backgroundTag":"invalid-argument-value","analyzedSha":"0152f468fc9463ee02dc2ca83f6fe4989a2c4ca5","analyzedAt":"2026-09-12T14:03:37.243Z","contentChangedAt":"2026-09-12T14:03:37.243Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}