Blankj / AndroidUtilCode

:fire: Android developers should collect the following utils(updating).
https://blankj.com/2016/07/31/android-utils-code/
Apache License 2.0
33.24k stars 10.67k forks source link

ToastUtils.showShort()会造成NPE异常 #512

Closed Kong-xyZ closed 6 years ago

Kong-xyZ commented 6 years ago

main(1)

java.lang.NullPointerException Argument 'text' of type CharSequence (#0 out of 1, zero-based) is marked by @android.support.annotation.NonNull but got null for it

1 java.lang.IllegalStateException:Fatal Exception thrown on Scheduler.Worker thread.

2 rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:59) 3 ...... 4 Caused by: 5 java.lang.NullPointerException:Argument 'text' of type CharSequence (#0 out of 1, zero-based) is marked by @android.support.annotation.NonNull but got null for it 6 com.blankj.utilcode.util.ToastUtils.showShort(Unknown Source)

Blankj commented 6 years ago

你传入了什么鸡儿参数?

Kong-xyZ commented 6 years ago

服务器返回的一个字符串

Blankj commented 6 years ago

问题回答得准确点,具体是什么,看结果肯定是你传入了空字符串造成的,这是你自己的问题哦

Kong-xyZ commented 6 years ago

嗯,应该是服务器返回的数据问题,返回的数据为null,传到你的Toast就出现这个问题了。

mnb65482 commented 6 years ago

一般会直接把服务端返回的错误信息直接show出来,在遇到返回为null的情况下,再调用ToastUtils.showLong()方法就应用崩溃了,有什么办法规避呢?

Blankj commented 6 years ago

if else 判断下不就好了么 我也可以考虑传入空显示 null

Kong-xyZ commented 6 years ago

跟后端同事约定,不传入null,或者本地设置一个默认错误提示,然后判断是否为null

mnb65482 commented 6 years ago

由于项目接入了多种外部API,所以返回协议不统一,导致无法控制返回是否为null。如果在每个返回里都去做if else或者都去给默认值的话,后面接手项目的开发者不知道有这坑的话,就麻烦了。柯基 大神有没有考虑处理下呢?

Blankj commented 6 years ago

用 1.18.6 做了空处理

mnb65482 commented 6 years ago

好的。多谢大神