如何从Activity转到PreferenceFragment? [英] How to go from an Activity to PreferenceFragment?

查看:104
本文介绍了如何从Activity转到PreferenceFragment?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从我的主页(活动为 Activity )导航到使用 PreferenceFragment 的帐户设置页面.

I am having trouble navigating from my main page which is an Activity to the account settings page which uses PreferenceFragment.

我正在尝试通过导航抽屉实现这一点,当单击该项目时,应显示设置页面.除帐户设置页面外,我所有其他页面均为 Fragment ,并且所有页面均正常工作(设置页面除外).我以为 PreferenceFragment Fragment 的扩展,因此它肯定可以工作吗?

I'm trying to achieve this via the navigation drawer, when the item is clicked, the settings page should be displayed. All my other pages are a Fragment except the account settings page and all work properly (except for the settings page). I thought PreferenceFragment extends from Fragment so surely it should work?

MainPage.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();
    if (position == 0) {
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(1))
                .commit();

    } else if (position == 1) {
        fragmentManager.beginTransaction()
                .replace(R.id.container, Search.newInstance(2))
                .commit();

    }

    else if (position == 2) {
        fragmentManager.beginTransaction()
                .replace(R.id.container, Favourites.newInstance(3))
                .commit();
    }

    else if (position == 3) {
        fragmentManager.beginTransaction()
                .replace(R.id.container, History.newInstance(4))
                .commit();

    }

    else if (position == 4) {
        fragmentManager.beginTransaction()
                .replace(R.id.container, AccountSettings.newInstance(5))
                .commit();

    }

}

AccountSettings.java

public class AccountSettings extends PreferenceFragment {

    Activity mActivity;

    private static final String ARG_SECTION_NUMBER = "section_number";

    public static AccountSettings newInstance(int sectionNumber) {
        AccountSettings fragment = new AccountSettings();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    //inflating layout
        addPreferencesFromResource(R.xml.settings);
        setHasOptionsMenu(true);
        restoreActionBar();

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;

    }

    public void restoreActionBar() {
        ActionBar actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("Account Settings");
    }

    public AccountSettings() {

    }
}

这会在 MainPage.java 上给我以下错误:

It would give me the following error on MainPage.java:

'replace(int, android.support.v4.app.Fragment)' in 'android.support.v4.app.FragmentTransaction' cannot be applied to '(int, com.example.laptop.whatsfordinner.AccountSettings)'

我尝试将其更改为 Fragment ,并且可以使用,但是我想使用 PreferenceFragment ,那么如何使它起作用?

I've tried changing it to a Fragment and it works, but I want to use PreferenceFragment, so how can I get it to work?

我肯定会遗漏一些确实很明显的东西吗?

Surely I'm just missing something really obvious?

推荐答案

您需要将 getFragmentManager 用于PreferenceFragment.因为它们不扩展Fragment类的支持v4版本.

You need to use getFragmentManager for PreferenceFragment. Because they don't extend the support v4 version of Fragment class.

这篇关于如何从Activity转到PreferenceFragment?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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