类型不匹配:不能转换ListFragment到碎片 [英] Type mismatch: cannot convert from ListFragment to Fragment
问题描述
我有如下一个 TabsPagerAdapter
类。类FirstFragment,SecondFragment和ThirdFragment每个扩展片段
键,一切都很好。
然后,我改变了SecondFragment延长 ListFragment
,现在我得到的编译错误:结果
的类型不匹配:不能从SecondFragment转换为片段的
ListFragment
扩展片段
,所以在我的脑海里。这不应该是一个问题。结果
我该怎么办?
在SecondFragment进口是:
进口android.app.ListFragment;
类:
公共类TabsPagerAdapter扩展FragmentPagerAdapter { 私人语境mContext; 公共TabsPagerAdapter(FragmentManager fragmentManager,上下文的背景下){
超(fragmentManager);
mContext =背景;
} @覆盖
公共片段的getItem(INT指数){ 片段returnFragment = NULL; 开关(指数){
情况下0:
returnFragment =新FirstFragment(mContext);
打破;
情况1:
returnFragment =新SecondFragment(mContext);
打破;
案例2:
returnFragment =新ThirdFragment(mContext);
打破;
} 返回returnFragment;
} @覆盖
公众诠释的getCount(){
//获取项目计数 - 等于标签数
返回3;
}
}
这个问题通常是由于在不同的类进口之间的不匹配。你可能有一类天然片段
,并从支持库中的其他的人。修复导入,也将努力。
检查进口。还删除需要一个参数在构造片段
子类。该系统想要一个公共的空构造(即默认构造函数)。如果你需要一个背景下,你可以使用 getActivity()
I have a TabsPagerAdapter
class as below. The classes FirstFragment, SecondFragment and ThirdFragment each extended Fragment
and all was well.
Then I changed SecondFragment to extend ListFragment
and now I get compile error:
Type mismatch: cannot convert from SecondFragment to Fragment
ListFragment
extends Fragment
, so in my mind. this shouldn't be a problem.
What do I do?
Import in SecondFragment is:
import android.app.ListFragment;
Class:
public class TabsPagerAdapter extends FragmentPagerAdapter {
private Context mContext;
public TabsPagerAdapter(FragmentManager fragmentManager, Context context) {
super(fragmentManager);
mContext = context;
}
@Override
public Fragment getItem(int index) {
Fragment returnFragment = null;
switch (index) {
case 0:
returnFragment = new FirstFragment(mContext);
break;
case 1:
returnFragment= new SecondFragment(mContext);
break;
case 2:
returnFragment = new ThirdFragment(mContext);
break;
}
return returnFragment;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
The issue is usually due of a mismatch between the import in the different classes. You probably have the native Fragment
in one class and the one from the support library in the other. Fix the imports and it will work.
check the imports. Also remove the constructor that takes a parameter in your Fragment
subclasses. The system wants a public empty constructor (aka default constructor). If you need a context, you can use getActivity()
这篇关于类型不匹配:不能转换ListFragment到碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!