Search code examples
javaandroidandroid-fragmentsfragmenttransaction

Error:(25, 94) error: incompatible types: SettingsFragment cannot be converted to Fragment


import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import static android.R.attr.fragment;

public class AppPreferences extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_detail);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        SettingsFragment settingsFragment = new SettingsFragment();
       // fragmentTransaction.add(android.R.id.content, settingsFragment, "SETTING_FRAGMENT");

the error starting here is settingsFragment and the error is Error:(25, 94) error: incompatible types: SettingsFragment cannot be converted to Fragment

FragmentTransaction setting_fragment = fragmentTransaction.add(android.R.id.content, settingsFragment, "SETTING_FRAGMENT");
setting_fragment.commit();

and this is my settingsFragment

public  static class SettingsFragment extends PreferenceFragment{

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.app_preferences);
    }
}

Solution

  • You should use (if you want to keep the compatibility) the PreferenceFragmentCompat included in the support v7 compatibility library.

    PreferenceFragment is not included in the compatibility set of fragments, so changing PreferenceFragment to PreferenceFragmentCompat should suffice for your porpuses.

    Check this out.

    Hope it helps