Type mismatch: cannot convert from MainFragment to Fragment 报错

时间:2023-01-12 13:49:00

源码:

1         FragmentTransaction mFragmentTranscation = getSupportFragmentManager().beginTransaction();
2         Fragment mMainFragment = new MainFragment();//此行提示出错
3         mFragmentTranscation.replace(R.id.container, mMainFragment);
4         mFragmentTranscation.commit();

 

错误提示:无法将MainFragment (Fragment的子类)强制转换成Fragment

解决方法:

在原来的MainFragment中把

import android.app.Fragment;

更改为:

import android.support.v4.app.Fragment;