我有一个带片段的应用。我无法让PreferenceFragment工作。其中一个片段是一个设置页面。为此,我希望使用PreferenceFragment但是我使用android.support.v4.app.FragmentManager开发了我的应用程序,它与PreferenceFragment不兼容。下面的链接有一个说明和指向可与v4一起使用的代码的链接。
https://github.com/codepath/android_guides/wiki/Settings-with-PreferenceFragment
https://github.com/kolavar/android-support-v4-preferencefragment
我的主要活动
private class CustomAdapter extends FragmentPagerAdapter {
private String fragments [] = {"Event","Gallery","Match","Settings"};
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new event();
case 1:
return new gallery();
case 2:
return new match();
case 3:
return new PreferenceFragment();
default:
return null;
}
}
错误:'PreferenceFragment'是抽象的,无法实例化。
PreferenceFragment
public abstract class PreferenceFragment extends Fragment implements
PreferenceManagerCompat.OnPreferenceTreeClickListener {
private static final String PREFERENCES_TAG = "android:preferences";
我被告知我无法直接实例化PreferenceFragment,我必须创建一个扩展它的类。有人可以解释一下我在这种情况下会怎样做吗?太感谢了! : - )
答案 0 :(得分:2)
例如:
public class MyPreferenceFragment extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_preference);
}
}
及其关联的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="FOO">
<CheckBoxPreference
android:key="checkBoxPref"
android:title="check it out"
android:summary="click this little box"/>
</PreferenceCategory>
</PreferenceScreen>
来源:How do you create Preference Activity and Preference Fragment on Android?