Fragment使用带参数的构造函数遇到的问题

Fragment使用带参数的构造函数,有时候会报如下错误:
android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.gw.PcOtherFragment: make sure class name exists, is public, and has an empty constructor that is public
 
  
所以,Android 官方是不推荐Fragment使用带参数的构造函数
官方推荐使用如下方法创建Fragment实例(带参数):
  public static MyFragment newInstance(int title) {
        MyFragment frag = new MyFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }

参考链接:http://developer.android.com/reference/android/app/Fragment.html

你可能感兴趣的:(Android,知识)