【发布时间】:2017-07-09 16:08:41
【问题描述】:
我想在相对布局中添加一个片段,这是我的 MainActivity.java :
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentOne f1 = new FragmentOne();
fragmentTransaction.add(R.id.fragment_container, f1);
fragmentTransaction.commit();
}
}
当我运行该代码时,我得到了这个错误:
Error:(15, 61) error: incompatible types: android.app.FragmentManager cannot be converted to android.support.v4.app.FragmentManager
如何将片段添加到相对布局中?我试图将getFragmentManager(); 更改为getSupportFragmentManager();
也没有用。知道这有什么问题吗?
更新:这是我将 getFragmentManager(); 更改为 getSupportFragmentManager(); 时得到的结果
【问题讨论】:
-
查看调用堆栈 - 您的活动没有实现您的片段期望的接口。见:developer.android.com/training/basics/fragments/…