|
1 | 1 | package com.blankj.utilcode.util;
|
2 | 2 |
|
3 | 3 | import android.content.res.Resources;
|
| 4 | +import android.support.annotation.ArrayRes; |
| 5 | +import android.support.annotation.StringRes; |
4 | 6 |
|
5 | 7 | /**
|
6 | 8 | * <pre>
|
@@ -194,57 +196,45 @@ public static String toSBC(final String s) {
|
194 | 196 | }
|
195 | 197 |
|
196 | 198 | /**
|
197 |
| - * Return the string value associated with a particular resource ID. It |
198 |
| - * will be stripped of any styled text information. |
199 |
| - * {@more} |
| 199 | + * Return the string value associated with a particular resource ID. |
200 | 200 | *
|
201 |
| - * @param id The desired resource identifier, as generated by the aapt |
202 |
| - * tool. This integer encodes the package, type, and resource |
203 |
| - * entry. The value 0 is an invalid identifier. |
204 |
| - * |
205 |
| - * @throws Resources.NotFoundException Throws NotFoundException if the given ID does not exist. |
206 |
| - * |
207 |
| - * @return String The string data associated with the resource, |
208 |
| - * stripped of styled text information. |
| 201 | + * @param id The desired resource identifier. |
| 202 | + * @return the string value associated with a particular resource ID. |
209 | 203 | */
|
210 |
| - public static String getString(int id) { |
211 |
| - return Utils.getApp().getResources().getString(id); |
| 204 | + public static String getString(@StringRes int id) { |
| 205 | + try { |
| 206 | + return Utils.getApp().getResources().getString(id); |
| 207 | + } catch (Resources.NotFoundException ignore) { |
| 208 | + return ""; |
| 209 | + } |
212 | 210 | }
|
213 | 211 |
|
214 | 212 | /**
|
215 |
| - * Return the string value associated with a particular resource ID, |
216 |
| - * substituting the format arguments as defined in {@link java.util.Formatter} |
217 |
| - * and {@link java.lang.String#format}. It will be stripped of any styled text |
218 |
| - * information. |
219 |
| - * {@more} |
220 |
| - * |
221 |
| - * @param id The desired resource identifier, as generated by the aapt |
222 |
| - * tool. This integer encodes the package, type, and resource |
223 |
| - * entry. The value 0 is an invalid identifier. |
| 213 | + * Return the string value associated with a particular resource ID. |
224 | 214 | *
|
| 215 | + * @param id The desired resource identifier. |
225 | 216 | * @param formatArgs The format arguments that will be used for substitution.
|
226 |
| - * |
227 |
| - * @throws Resources.NotFoundException Throws NotFoundException if the given ID does not exist. |
228 |
| - * |
229 |
| - * @return String The string data associated with the resource, |
230 |
| - * stripped of styled text information. |
| 217 | + * @return the string value associated with a particular resource ID. |
231 | 218 | */
|
232 |
| - public static String getString(int id, Object... formatArgs) { |
233 |
| - return Utils.getApp().getString(id, formatArgs); |
| 219 | + public static String getString(@StringRes int id, Object... formatArgs) { |
| 220 | + try { |
| 221 | + return Utils.getApp().getString(id, formatArgs); |
| 222 | + } catch (Resources.NotFoundException ignore) { |
| 223 | + return ""; |
| 224 | + } |
234 | 225 | }
|
235 | 226 |
|
236 | 227 | /**
|
237 | 228 | * Return the string array associated with a particular resource ID.
|
238 | 229 | *
|
239 |
| - * @param id The desired resource identifier, as generated by the aapt |
240 |
| - * tool. This integer encodes the package, type, and resource |
241 |
| - * entry. The value 0 is an invalid identifier. |
242 |
| - * |
243 |
| - * @throws Resources.NotFoundException Throws NotFoundException if the given ID does not exist. |
244 |
| - * |
| 230 | + * @param id The desired resource identifier. |
245 | 231 | * @return The string array associated with the resource.
|
246 | 232 | */
|
247 |
| - public static String[] getStringArray(int id){ |
248 |
| - return Utils.getApp().getResources().getStringArray(id); |
| 233 | + public static String[] getStringArray(@ArrayRes int id) { |
| 234 | + try { |
| 235 | + return Utils.getApp().getResources().getStringArray(id); |
| 236 | + } catch (Resources.NotFoundException ignore) { |
| 237 | + return new String[0]; |
| 238 | + } |
249 | 239 | }
|
250 | 240 | }
|
0 commit comments