创建片段时缺少参数

时间:2014-05-26 11:01:12

标签: java android android-fragments fragment

在我第一次尝试使用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();
   }

1 个答案:

答案 0 :(得分:0)

你的LM_Fragment应该继承Fragment类。同样在replace(int, fragment)中你应该传递容器ID。 Container是保存片段的视图,而不是片段id。