原文博客链接:https://blog.csdn.net/chniccs/article/details/51258972

在创建fragment时,你可能在打包时碰到如下错误

Error:Error: Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead [ValidFragment]

其原因是你重载了fragment的构造方法,但是在一些情况下,如屏幕翻转时,fragment被重新创建,就可能会造成数据丢失。

解决方案一(不推荐):@SuppressLint({"NewApi", "ValidFragment"})

在构造方法上加上这个注解,就可以不检察,但是这是google不推荐的做法

解决方案二(也不推荐):不报错

  1. lintOptions {
  2.  
  3. abortOnError false
  4.  
  5. }

这段加到项目的gradle文件中就可以不报这个错误了,不过这只是让他不报而已,实际上还是存在问题的,所以也不推荐这样做。

解决方案三(推荐):创建newInstance方法来

这种方式避免了使用构造来传参数
 

创建newInstance方法

  1. public static final MessageAlreadyReadFragment newInstance(long mGroupId)
  2. {
  3.  
  4. MessageAlreadyReadFragment fragment = new MessageAlreadyReadFragment();
  5.  
  6. Bundle bundle = new Bundle();
  7.  
  8. bundle.putLong("mGroupId",mGroupId);
  9.  
  10. fragment.setArguments(bundle);
  11.  
  12. return fragment ;
  13.  
  14. }

重写onCreate

  1. @Override
  2.  
  3. public void onCreate(@Nullable Bundle savedInstanceState) {
  4.  
  5. super.onCreate(savedInstanceState);
  1. mGroupId=getArguments().getLong("mGroupId");
  1. }

创建fragment实例

 
  1. mAlreadyRead = MessageAlreadyReadFragment.newInstance(groupIdForReceipt);

通过上面的这种方式就可以解决问题。

Error:Error: Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead [ValidFragment]的更多相关文章

  1. Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead

    “Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(B ...

  2. “Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle)instead”

    “Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(B ...

  3. Fragment:关于Avoid non-default constructors in fragments的错误

    在android开发中,写了一个关于继承Fragment的类时,如果有重载构造函数时,会提示“Avoid non-default constructors in fragments: use a de ...

  4. gcc 错误:Fatal error error writing to tmp No space left on device

    在使用gcc make时报错:Fatal error error writing to tmp No space left on device finiteVolume/ddtSchemes/Eule ...

  5. 在执行gem install redis时 : ERROR: Error installing redis: redis requires Ruby version >= 2.2.2

    在执行gem install redis时 提示: gem install redis ERROR: Error installing redis: redis requires Ruby versi ...

  6. Spring mvc 中使用ftl引用共通文件出错 FreeMarker template error: Error reading included file "/WEB-INF/ftl/common/errormessage.ftl"

    初次接触spring mvc,想做一个小的练习项目,结果在ftl文件中引用其它的共通ftl文件时出错.

  7. error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.

    error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCom ...

  8. Error: Error setting TTL index on collection : sessions

    Error: Error setting TTL index on collection : sessions 一.步骤一: 这个问题一般是直接升级 mongodb和connect-mongo的版本为 ...

  9. 【android】新手容易遇到的[error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.]Theme出错的问题

    一.概述 近期刚接手了一个项目,开发工具为eclipse,由于版本较低,且考虑到如果转android studio项目的话,会其他人的维护带来困难,所以想着还是维护项目原来的开发环境吧. 但是导入项目 ...

随机推荐

  1. WPF开发ArcGis系统时的异常信息: ArcGIS product not specified. You must first bind to an ArcGIS version prior to using any ArcGIS components.

    “System.Runtime.InteropServices.COMException”类型的未经处理的异常在 Arcgis_Test.exe 中发生 其他信息: ArcGIS product no ...

  2. Maven依赖下载速度慢,不用怕,这么搞快了飞起

    一.背景 众所周知,Maven对于依赖的管理让我们程序员感觉爽的不要不要的,但是由于这货是国外出的,所以在我们从中央仓库下载依赖的时候,速度如蜗牛一般,让人不能忍,并且这也是大多数程序员都会遇到的问题 ...

  3. ubantu 上hadoop 搭建

    Hadoop安装教程_单机/伪分布式配置_Hadoop2.6.0/Ubuntu14.04 参考 http://www.powerxing.com/install-hadoop/ 2014-08-09 ...

  4. uiautomator 代码记录 :BT接收测试

    package rom; import java.lang.*; import java.util.Random; import java.io.File; import com.android.ui ...

  5. TCP/UDP模型

    1网络接口层 MAC地址2网络互联层 IP地址3传输层 TCP/UDP端口号4应用层 应用层协议

  6. ip黑白名单防火墙frdev的原理与实现

    汤之盘铭曰 苟日新 日日新 又日新 康诰曰 作新民 诗曰 周虽旧邦 其命维新 是故 君子无所不用其极 ——礼记·大学 在上一篇文章<DDoS攻防战 (二) :CC攻击工具实现与防御理论>中 ...

  7. 开源小程序CMS网站, JeeWx-App-CMS 1.0 首版本发布

    JeeWx-App-CMS 是jeewx开发的小程序网站开源项目,基于小程序wepy语言,具备cms网站的基本功能,能够打造简单易用的小程序公司官网.项目结构简单,逻辑清晰,代码规范,非常适合作为小程 ...

  8. nodejs 解决跨域

    1.失败 app.all('*', function (req, res, next) { res.header("Access-Control-Allow-Origin", &q ...

  9. python oracle 查询返回字典

    from: https://sourceforge.net/p/cx-oracle/mailman/message/27145597/ I'd do it with a "row facto ...

  10. Weed-FS 接口 master、volume 服务接口(转)

    目录结构   weed-fs master 服务接口,分配文件 id,查找 volume,volume 服务接口,在指定的 volume 服务创建指定的 volume,检查 volume 服务的状态. ...