无法使PreferenceFragment工作(编译错误) [英] Can't get the PreferenceFragment to work (Compiling error)

查看:159
本文介绍了无法使PreferenceFragment工作(编译错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是当我尝试编译我的项目时收到错误:FragmentTransaction类型中的方法add(int,Fragment)不适用于参数(int,FragmentPreferences)。据我所知,PreferenceFragment是一个片段?我正在为Android 4.0开发,但我必须使用android-support-v4库,因为我的项目还包括ViewPager。我在 http://developer.android.com/guide中找到了这个例子/topics/ui/settings.html#Fragment

I am getting error when i try to compile my project: "The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, FragmentPreferences)". As far as i know, the PreferenceFragment is a Fragment? I am developing for android 4.0 but i have to use android-support-v4 library because my project incluedes also ViewPager. I found this example at http://developer.android.com/guide/topics/ui/settings.html#Fragment:

// Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();

但我无法让它发挥作用。

But i can't get it to work.

我的代码:

case R.id.menu_settings:
    FragmentPreferences prefs = new FragmentPreferences();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(android.R.id.content, prefs);
    ft.commit();
    break;

FragmentPreferences:

FragmentPreferences:

public class FragmentPreferences extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

}


推荐答案

那是因为 PreferenceFragment 扩展 android.app.Fragment ,而不是 android。 support.v4.app.Fragment android.support.v4.app.FragmentTransaction 期待。

That's because PreferenceFragment extends android.app.Fragment, rather than the android.support.v4.app.Fragment that the android.support.v4.app.FragmentTransaction is expecting.

截至目前,支持库中没有 PreferenceFragment 的官方反向端口。但是,您可以使用 UnifiedPreferences 等库来填补这一空白。

As of yet, there is no official backport of the PreferenceFragment to the support library. However, you can use libraries such as UnifiedPreferences to fill this gap.

这篇关于无法使PreferenceFragment工作(编译错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆