在事务替换方法中获取错误

时间:2018-05-06 12:09:21

标签: android android-fragments fragmenttransaction

我正在为Fragment事务创建一个方法,我必须用片段替换容器。

 ProfileFragment fragment = new ProfileFragment();
        FragmentTransaction transaction = ProfileActivity.this.getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.container, fragment);
        transaction.addToBackStack(getString(R.string.profile_fragment));
        transaction.commit();

我正在学习的教程工作正常,但我收到错误,我已经检查了导入语句,他们也可能是同样的错误?

Error:(48, 45) error: incompatible types: ProfileFragment cannot be converted to Fragment

1 个答案:

答案 0 :(得分:1)

而不是:

FragmentTransaction transaction = ProfileActivity.this.getSupportFragmentManager().beginTransaction(); 

把这个:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();