我想要將某個活動的某些值傳遞給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