关于fragment在打包时找不到一个类的情况:

报错信息:Error : Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead:

原因:


Because of the nature of fragment and how there managed and shown on your screen it's very recommended not to create a non default constructor and in many cases this would cause problems in run time. In case you should do something like this:

因为原生的fragment的管理和在屏幕上显示,是推荐不要在任何继承fragment类里面,去创建类的构造方法,否则在运行时,会出现问题。

A side note for future readers: if your Fragment subclass doesn't declare any constructors at all, then by default an empty public constructor will implicitly be made for you (this is standard Java behavior‌ ). You do not have to explicitly declare an empty constructor unless you also declared other constructors (e.g. ones with arguments).

如果你的fragment没有声明任何的构造函数,系统会自动帮你创建一个构造器。除非你有声明一个带参的构造函数,才需要声明一个无参的构造函数。


而你应该按照如下的来做:

public static final GridFragment newInstance(String tabId)

{

    GridFragment f = new GridFragment();

    Bundle bdl = new Bundle(2);

    bdl.putString(TAB_ID, tabId);

    f.setArguments(bdl);

    return f;

}

and in onCreate extract the needed data from the bundle:

@Override

public void onCreate(Bundle savedInstanceState) 

{

    String tabId = getArguments().getString(TAB_ID);

    }



解决:

if you like to be out of rules just do next

@SuppressLint("ValidFragment")

public PlaceDialogFragment(Place place, DisplayMetrics dm){

        super();

        this.mPlace = place;

        this.mMetrics = dm;

    }

仅仅是警告,避免使用非默认构造函数,因为Fragment源码中用到反射构造了对象,是无参数的构造函数。可以在ShowSynopsis上面加上@SuppressLint("ValidFragment"),忽略警告。

或者加入这句代码@SuppressLint(“ValidFragment")


更多相关文章

  1. 箭头函数的基础使用
  2. Python技巧匿名函数、回调函数和高阶函数
  3. 浅析android通过jni控制service服务程序的简易流程
  4. Android(安卓)bluetooth介绍(四): a2dp connect流程分析
  5. Android架构分析之使用自定义硬件抽象层(HAL)模块
  6. Android中OpenMax的适配层
  7. android 包管理系统分析
  8. Android中获取屏幕相关信息(屏幕大小,状态栏、标题栏高度)
  9. Android(安卓)启动过程(2)

随机推荐

  1. Android学习《Test and intrumentation》
  2. android新建项目时gen目录下的R文件没有
  3. 探索 Android(安卓)中的 Span
  4. Android系统数据传递机制
  5. 使用Android(安卓)隐藏API和内部 API
  6. 命令行开发、编译、打包Android应用程序
  7. android EditText基本设置
  8. android布局中的基本属性:
  9. Android(安卓)NDK 编程环境搭建
  10. [置顶] Android常用适配器控件