user1888162
user1888162

Reputation: 1735

Can't get the PreferenceFragment to work (Compiling error)

enter image description here

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.

My code:

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

FragmentPreferences:

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

}

Upvotes: 5

Views: 7687

Answers (3)

JR Tan
JR Tan

Reputation: 1725

Here is the unofficial backport of the PreferenceFragment to the support library. Last update was Feb 2015 & it's compatible with lollipop devices. android-support-v4-preferencefragment

import android.support.v4.preference.PreferenceFragment;

Upvotes: 0

ianhanniballake
ianhanniballake

Reputation: 199805

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.

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.

Upvotes: 16

Oleg Vaskevich
Oleg Vaskevich

Reputation: 12672

Check the import statements at the top of your code. Likely one of the classes you are using is from support while the other is not.

Upvotes: 1

Related Questions