在创建fragment时,你可能在打包时碰到如下错误
Error: Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead [ValidFragment] 或者 Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle)
and later retrieved by the Fragment with getArguments()
.
其原因是你重载了fragment的构造方法,但是在一些情况下,如屏幕翻转时,fragment被重新创建,就可能会造成数据丢失。
转载:http://blog.csdn.net/chniccs/article/details/51258972
解决方案一(不推荐):@SuppressLint({"NewApi", "ValidFragment"})
在构造方法上加上这个注解,就可以不检察,但是这是google不推荐的做法
解决方案二(也不推荐):不报错
这段加到项目的gradle文件中就可以不报这个错误了,不过这只是让他不报而已,实际上还是存在问题的,所以也不推荐这样做。