我正在开发一个Android应用程序,它突然给了我这些错误(它曾经像这样工作,这是奇怪的部分):
避免片段中的非默认构造函数:使用默认构造函数加上Fragment #setArguments(Bundle)而不是
和
此片段应提供默认构造函数(不带参数的公共构造函数)
这是代码:
public DatePickerFragment(ProjectOverviewFragment list){
this.list = list;
Calendar cal = Calendar.getInstance();
date = cal.get(Calendar.DAY_OF_MONTH)+"-"+cal.get(Calendar.MONTH)+"-"+cal.get(Calendar.YEAR);
}
答案 0 :(得分:4)
你必须调用像这样的片段:
int id;
Fragment newFragment = CountingFragment.newInstance(id);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.simple_fragment, newFragment);
ft.addToBackStack(null);
ft.commit();
public static class CountingFragment extends Fragment {
int mNum;
static CountingFragment newInstance(int num) {
CountingFragment f = new CountingFragment();
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Hello. This is fragment example #" + mNum);
tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
return v;
}
}