@@ -39,6 +39,7 @@ public final class ToastUtils {
39
39
40
40
private static final int COLOR_DEFAULT = 0xFEFFFFFF ;
41
41
private static final Handler HANDLER = new Handler (Looper .getMainLooper ());
42
+ private static final String NULL = "null" ;
42
43
43
44
private static Toast sToast ;
44
45
private static int sGravity = -1 ;
@@ -107,8 +108,8 @@ public static void setMsgTextSize(final int textSize) {
107
108
*
108
109
* @param text The text.
109
110
*/
110
- public static void showShort (@ NonNull final CharSequence text ) {
111
- show (text , Toast .LENGTH_SHORT );
111
+ public static void showShort (final CharSequence text ) {
112
+ show (text == null ? NULL : text , Toast .LENGTH_SHORT );
112
113
}
113
114
114
115
/**
@@ -127,11 +128,7 @@ public static void showShort(@StringRes final int resId) {
127
128
* @param args The args.
128
129
*/
129
130
public static void showShort (@ StringRes final int resId , final Object ... args ) {
130
- if (args != null && args .length == 0 ) {
131
- show (resId , Toast .LENGTH_SHORT );
132
- } else {
133
- show (resId , Toast .LENGTH_SHORT , args );
134
- }
131
+ show (resId , Toast .LENGTH_SHORT , args );
135
132
}
136
133
137
134
/**
@@ -141,20 +138,16 @@ public static void showShort(@StringRes final int resId, final Object... args) {
141
138
* @param args The args.
142
139
*/
143
140
public static void showShort (final String format , final Object ... args ) {
144
- if (args != null && args .length == 0 ) {
145
- show (format , Toast .LENGTH_SHORT );
146
- } else {
147
- show (format , Toast .LENGTH_SHORT , args );
148
- }
141
+ show (format , Toast .LENGTH_SHORT , args );
149
142
}
150
143
151
144
/**
152
145
* Show the sToast for a long period of time.
153
146
*
154
147
* @param text The text.
155
148
*/
156
- public static void showLong (@ NonNull final CharSequence text ) {
157
- show (text , Toast .LENGTH_LONG );
149
+ public static void showLong (final CharSequence text ) {
150
+ show (text == null ? NULL : text , Toast .LENGTH_LONG );
158
151
}
159
152
160
153
/**
@@ -173,11 +166,7 @@ public static void showLong(@StringRes final int resId) {
173
166
* @param args The args.
174
167
*/
175
168
public static void showLong (@ StringRes final int resId , final Object ... args ) {
176
- if (args != null && args .length == 0 ) {
177
- show (resId , Toast .LENGTH_SHORT );
178
- } else {
179
- show (resId , Toast .LENGTH_LONG , args );
180
- }
169
+ show (resId , Toast .LENGTH_LONG , args );
181
170
}
182
171
183
172
/**
@@ -187,11 +176,7 @@ public static void showLong(@StringRes final int resId, final Object... args) {
187
176
* @param args The args.
188
177
*/
189
178
public static void showLong (final String format , final Object ... args ) {
190
- if (args != null && args .length == 0 ) {
191
- show (format , Toast .LENGTH_SHORT );
192
- } else {
193
- show (format , Toast .LENGTH_LONG , args );
194
- }
179
+ show (format , Toast .LENGTH_LONG , args );
195
180
}
196
181
197
182
/**
@@ -234,7 +219,16 @@ private static void show(@StringRes final int resId, final int duration, final O
234
219
}
235
220
236
221
private static void show (final String format , final int duration , final Object ... args ) {
237
- show (String .format (format , args ), duration );
222
+ String text ;
223
+ if (format == null ) {
224
+ text = NULL ;
225
+ } else {
226
+ text = String .format (format , args );
227
+ if (text == null ) {
228
+ text = NULL ;
229
+ }
230
+ }
231
+ show (text , duration );
238
232
}
239
233
240
234
private static void show (final CharSequence text , final int duration ) {
@@ -280,6 +274,7 @@ public void run() {
280
274
private static void showToast () {
281
275
if (Build .VERSION .SDK_INT == Build .VERSION_CODES .N_MR1 ) {
282
276
try {
277
+ //noinspection JavaReflectionMemberAccess
283
278
Field field = View .class .getDeclaredField ("mContext" );
284
279
field .setAccessible (true );
285
280
field .set (sToast .getView (), new ApplicationContextWrapperForApi25 ());
0 commit comments