简体   繁体   中英

Preference Activity

I am creating a settings activity for my android application, one of the options it to allow password or not, I do not just want that option to be checked anyhow so I want it to ask for a password before changing the preference. I want a Dialog fragment to pop up and ask for the password before the preference changes, after changing, the onPreferenceChange method should be called and if the password is correct the preference can change by returning true ;

The issue is that my settings fragment cannot recognize getFragmentManager() and even if I do getActivity.getFragmentManager it gives an error that the preferencefragment cannot be cast to the dialog . The dialog works for normal activities in the project but not the preference fragment.

This is the dialogfragment showing where the activity is to be casted

public class password_dialog extends DialogFragment {
public interface PasswordDialogListener {
    void onDialogPositiveClick(DialogFragment dialog, String password);

    void onDialogNegativeClick(DialogFragment dialog);
}

PasswordDialogListener mListener;


@Override
public void onAttach(Context activity) {
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try {
        // Instantiate the PasswordDialogListener so we can send events to the host
        mListener = (PasswordDialogListener) activity;
    } catch (Exception e) {
      Log.e("dialog",e.getMessage());
    }
}

. . .

This is the main code of the preference fragment the class extends extends PreferenceFragmentCompat and implements Preference.OnPreferenceChangeListener, password_dialog.PasswordDialogListener

 @Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    addPreferencesFromResource(R.xml.preferences);
    Preference preference = findPreference(getString(R.string.printSetting));

    /*****Realm*****/
    Realm.init(getActivity());
    realm = Realm.getDefaultInstance();

    preference.setOnPreferenceChangeListener(this);
}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
    DialogFragment PasswordDialogFragment = new password_dialog();
    PasswordDialogFragment.show(getFragmentManager(), "Password");

    if (passwordIsCorrect) {
        return true;
    } else {
        return false;
    }

}

And because of that it gives a null pointer exception because mListener is not initialized.

Since you are only facing the error to getFragmentManager() .

try to change your code from getFragmentManager() to getSupportFragmentManager() instead.

hope this will help.

You're mixing support and not support classes. When you're importing a class IDE suggest you to import one of two classes with the same name:

在此处输入图片说明

As you already use PreferenceFragmentCompat (which is class from support library) you should use support fragment related classes:

import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;

public class PreferenceFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceChangeListener {

    @Override public boolean onPreferenceChange(Preference preference, Object o) {
        FragmentManager fragmentManager = getFragmentManager();
        DialogFragment PasswordDialogFragment = get();
        PasswordDialogFragment.show(fragmentManager, "Password");
        return true;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM