本文介绍了首选项片段选项卡适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为我的应用程序构建一个首选项选项卡,但是由于我还有其他从常规片段扩展的选项卡,因此我遇到了不兼容的类型错误,因为PreferenceFragment无法转换为Fragment.
这是我的Tab片段适配器:
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1Discover tab1 = new Tab1Discover();
return tab1;
case 1:
Tab2Planning tab2 = new Tab2Planning();
return tab2;
case 2:
Tab3Favorites tab3 = new Tab3Favorites();
return tab3;
case 3:
Tab4Messages tab4 = new Tab4Messages();
return tab4;
case 4 :
/*Tab5Profile tab5 = new Tab5Profile();
return tab5;*/
return new PreferenceFragment() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
};
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
解决方案
类型不兼容,问题是-您的片段是从support.v4.app.Fragment继承的,而PreferenceFragment是从android.app.Fragment继承的. >
有两种处理方法:
- 从android.app.Fragment继承所有片段 通过从此处,并从support.v4.app.Fragment 继承
I want to build a preference tab for my app but since I have other tabs extending from a regular fragment, I have an incompatible types error, as a PreferenceFragment cannot be converted to Fragment.
Here is my Tab Fragment Adapter :
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1Discover tab1 = new Tab1Discover();
return tab1;
case 1:
Tab2Planning tab2 = new Tab2Planning();
return tab2;
case 2:
Tab3Favorites tab3 = new Tab3Favorites();
return tab3;
case 3:
Tab4Messages tab4 = new Tab4Messages();
return tab4;
case 4 :
/*Tab5Profile tab5 = new Tab5Profile();
return tab5;*/
return new PreferenceFragment() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
};
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
解决方案
There are incompatible types, the problem is - your fragments inherited from support.v4.app.Fragment, but PreferenceFragment inherited from android.app.Fragment.
There are two ways you could deal with it:
- Inherit all your fragments from android.app.Fragment
- Reimplement PreferenceFragment by taken it source code from here for example and inherit it from support.v4.app.Fragment
这篇关于首选项片段选项卡适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!