From 20fa86730f07788ecb705cfab91691d18a59970e Mon Sep 17 00:00:00 2001 From: Blankj <625783482@qq.com> Date: Mon, 12 Nov 2018 13:33:31 +0800 Subject: [PATCH] See 11/12 log --- .../com/blankj/utilcode/util/StringUtils.java | 64 ++++++++----------- 1 file changed, 27 insertions(+), 37 deletions(-) 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 daef9129..c3254e3d 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];
+        }
     }
 }
-- 
GitLab