关于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. Android Audio代码分析18 - setSampleRate函数
  2. Android Audio代码分析22 - AudioEffect::getEnabled函数
  3. Android Chronometer控件实现计时器函数详解
  4. Android下使用dlopen函数动态调用.so链接库
  5. Delphi Android USB声明文件
  6. Android Activity的onCreate()函数
  7. Android RectF类的构造函数参数说明
  8. android 读取SD卡或者其他地方文件功能函数

随机推荐

  1. Android 5.0新控件 FloatingActionButton
  2. Android之用PopupWindow实现弹出listview
  3. 今天发现的一些优秀的资源网站
  4. Simulate a key press on Android
  5. ffmpeg-2.3.3 configure for android
  6. 改变分隔条,基于数组的ListView
  7. Android 关于wifi管理的代码
  8. Android(安卓)下载一个文件以及打开这个a
  9. Android 照相机转到真确的方向
  10. android开发之onCreate( )方法详解