diff --git a/utilcode/src/main/java/com/blankj/utilcode/util/StringUtils.java b/utilcode/src/main/java/com/blankj/utilcode/util/StringUtils.java index daef9129761f0d9412eec7b50aca3f4b5c790434..c3254e3d32017e8ca32fc60bf2d608b54737e6fb 100644 --- a/utilcode/src/main/java/com/blankj/utilcode/util/StringUtils.java +++ b/utilcode/src/main/java/com/blankj/utilcode/util/StringUtils.java @@ -1,6 +1,8 @@ package com.blankj.utilcode.util; import android.content.res.Resources; +import android.support.annotation.ArrayRes; +import android.support.annotation.StringRes; /** *
@@ -194,57 +196,45 @@ public final class StringUtils { } /** - * Return the string value associated with a particular resource ID. It - * will be stripped of any styled text information. - * {@more} + * Return the string value associated with a particular resource ID. * - * @param id The desired resource identifier, as generated by the aapt - * tool. This integer encodes the package, type, and resource - * entry. The value 0 is an invalid identifier. - * - * @throws Resources.NotFoundException Throws NotFoundException if the given ID does not exist. - * - * @return String The string data associated with the resource, - * stripped of styled text information. + * @param id The desired resource identifier. + * @return the string value associated with a particular resource ID. */ - public static String getString(int id) { - return Utils.getApp().getResources().getString(id); + public static String getString(@StringRes int id) { + try { + return Utils.getApp().getResources().getString(id); + } catch (Resources.NotFoundException ignore) { + return ""; + } } /** - * Return the string value associated with a particular resource ID, - * substituting the format arguments as defined in {@link java.util.Formatter} - * and {@link java.lang.String#format}. It will be stripped of any styled text - * information. - * {@more} - * - * @param id The desired resource identifier, as generated by the aapt - * tool. This integer encodes the package, type, and resource - * entry. The value 0 is an invalid identifier. + * Return the string value associated with a particular resource ID. * + * @param id The desired resource identifier. * @param formatArgs The format arguments that will be used for substitution. - * - * @throws Resources.NotFoundException Throws NotFoundException if the given ID does not exist. - * - * @return String The string data associated with the resource, - * stripped of styled text information. + * @return the string value associated with a particular resource ID. */ - public static String getString(int id, Object... formatArgs) { - return Utils.getApp().getString(id, formatArgs); + public static String getString(@StringRes int id, Object... formatArgs) { + try { + return Utils.getApp().getString(id, formatArgs); + } catch (Resources.NotFoundException ignore) { + return ""; + } } /** * Return the string array associated with a particular resource ID. * - * @param id The desired resource identifier, as generated by the aapt - * tool. This integer encodes the package, type, and resource - * entry. The value 0 is an invalid identifier. - * - * @throws Resources.NotFoundException Throws NotFoundException if the given ID does not exist. - * + * @param id The desired resource identifier. * @return The string array associated with the resource. */ - public static String[] getStringArray(int id){ - return Utils.getApp().getResources().getStringArray(id); + public static String[] getStringArray(@ArrayRes int id) { + try { + return Utils.getApp().getResources().getStringArray(id); + } catch (Resources.NotFoundException ignore) { + return new String[0]; + } } }