我想要将某个活动的某些值传递给android中的片段。 下面是一些代码: -将活动值传递给片段android
MainActivity: -
PlaceDialogFragment dialogFragment = new PlaceDialogFragment(place,dm);
片段: -
public PlaceDialogFragment(Place place, DisplayMetrics dm){
super();
this.mPlace = place;
this.mMetrics = dm;
}
使用的片段构造给我一个错误: -
Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead
如果我将构造函数替换为: -
public static final DialogFragment newInstance(Place place, DisplayMetrics dm)
{
DialogFragment fragment = new DialogFragment();
Bundle bundle = new Bundle(2);
bundle.putParcelable("Place", place);
bundle.putFloat("Metrics", dm.density);
fragment.setArguments(bundle);
return fragment ;
}
我在MainActivity.Java得到一个错误: -
The constructor PlaceDialogFragment(Place, DisplayMetrics) is undefined
我如何解决这个问题?
片段应无参数的构造函数。亚历克斯回答你的问题看看@ http://developer.android.com/reference/android/app/DialogFragment.html – Raghunandan