在我第一次尝试使用Fragments
时,方法replace
会出现此错误The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, LM_Fragment)
我提供了第一个id
的{{1}} fragment
{1}}作为lm_fragment
方法的第一个参数。那是对的吗?如果不是我应该用.replace
方法提供什么参数?
MainActivity:
.replace
MainActivity xml文件:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
LM_Fragment lm_fragment = new LM_Fragment();
fragmentTransaction.replace(R.id.lm_fragment, lm_fragment);
}else{
PM_Fragment pm_fragment = new PM_Fragment();
fragmentTransaction.replace(R.id.pm_fragment, pm_fragment);
}
fragmentTransaction.commit();
}
答案 0 :(得分:0)
你的LM_Fragment应该继承Fragment类。同样在replace(int, fragment)
中你应该传递容器ID。 Container是保存片段的视图,而不是片段id。