Android Bundle getString方法注意

    /**
     * Returns the value associated with the given key, or null if
     * no mapping of the desired type exists for the given key or a null
     * value is explicitly associated with the key.
     *
     * @param key a String, or null
     * @return a String value, or null
     */
    @Nullable
    public String getString(@Nullable String key) {
        unparcel();
        final Object o = mMap.get(key);
        try {
            return (String) o;
        } catch (ClassCastException e) {
            typeWarning(key, o, "String", e);
            return null;
        }
    }

由于null强转String类型时,会变成“null”字符串,所以只有当转换失败时才会返回null,使用时需要注意对“null”字符串的判断。

2020-02-27

posted @ 2020-10-10 09:35  FirstLetterZ  阅读(990)  评论(0)    收藏  举报