0

I'm getting this error message "could not find fragment constructor" whenever I enter to my settings activity and rotate the device. Bellow is my settings activity onCreate() method and SettingsFragment class.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPreferences = 
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        setContentView(R.layout.settings_activity);
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.settings, new SettingsFragment(this))
                    .commit();
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

public static class SettingsFragment extends PreferenceFragmentCompat {
        WeakReference<SettingsActivity> activityWeakReference;
        SettingsFragment(SettingsActivity activity) {
            activityWeakReference = new WeakReference<>(activity);
        }
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
            ListPreference listPreference = findPreference("jojojojo");
            if (listPreference != null) {
                listPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                    @Override
                    public boolean onPreferenceChange(Preference preference, Object newValue) {
                        activityWeakReference.get().setResult(1);
                        return true;
                    }
                });
            }

            SwitchPreferenceCompat switchPreferenceCompat = findPreference("lalalala");
            if (switchPreferenceCompat != null) {
                switchPreferenceCompat.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
                    @Override
                    public boolean onPreferenceChange(Preference preference, Object newValue) {
                        if ((boolean)newValue) {
                            activityWeakReference.get().mostrarMensaje(getString(R.string.toastFrasesOn));
                            activityWeakReference.get().setResult(2);
                        } else {
                            activityWeakReference.get().setResult(3);
                        }
                        return true;
                    }
                });
            }
        }
    }
Mike087
  • 486
  • 3
  • 13

1 Answers1

0

I guess the problem here is that Fragment doesn't have a default constructor. Creating a parametrized constructor hides the default constructor. Also its not recommended to create a parametrized constructor.

All subclasses of Fragment must include a public no-argument constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the no-argument constructor is not available, a runtime exception will occur in some cases during state restore. Arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments().

To fix the problem

  1. Remove the current parametrized constructor in the SettingsFragment

  2. Add a no-arg constructor as follows:

    public SettingsFragment(){}

Its also not a good idea to pass activity reference to the fragment. A newer alternative to communicate between an Activity and a Fragment, thus allowing that the Fragment can have an empty constructor would be the use of ViewModels.

akhil nair
  • 1,371
  • 1
  • 11
  • 19