2

代码:

public PlacePickerFragment() {
    this(null);
}
public PlacePickerFragment(Bundle args) {
    super(GraphPlace.class, R.layout.com_facebook_placepickerfragment, args);
    setPlacePickerSettingsFromBundle(args);
}

您好,我想从上面的代码中删除弃用警告,有没有办法将其更改为默认构造函数?

4

3 回答 3

6

创建片段时,请使用 setArgument():

Bundle args = new Bundle();
// Construct your bundle here
Fragment mFragment = new PlacePickerFragment();
mFragment.setArguments(args);
mFragment.initialize();

并使用片段的默认构造函数。您可能需要setPlacePickerSettingsFromBundle()在设置参数后调用,如下所示:

public PlacePickerFragment() {
    super(GraphPlace.class, R.layout.com_facebook_placepickerfragment, args);
}

public void initialize() {
    Bundle args = getArguments();
    setPlacePickerSettingsFromBundle(args);
}
于 2012-11-21T03:35:18.290 回答
1

摆脱Bundle参数并使构造函数不带参数。然后使用setArguments()传递捆绑。如有必要,创建静态工厂方法以使用必要的参数创建您的片段。

于 2012-11-21T03:20:19.243 回答
0

Lawrence Choy 的回答非常有帮助,但对我不起作用,因为 super() 调用不接受 args 变量。这对我有用:

public void initialize() {
  Bundle args = getArguments();
  setPlacePickerSettingsFromBundle(args);
}

/**
  * Default constructor. Creates a Fragment with all default properties.
  */
public PlacePickerFragment() {
  super(GraphPlace.class, R.layout.com_facebook_placepickerfragment, null);
}
于 2014-12-22T09:33:41.900 回答