我的SharedPreferences为null。这是错误SharedPreferences.edit()是一个空对象引用

时间:2017-02-02 10:31:29

标签: android sharedpreferences android-sharedpreferences

我在我的应用程序中使用Model View Presenter,并尝试在SharedPreferences中保存值标记。但是,我得到了SharedPreferences null,错误是SharedPreferences.edit()是一个null对象引用。请帮我解决这个问题。谢谢

这是我的片段

p2

这是我的演讲者

public class SignUpFragment extends BaseFragment {

    @NotEmpty(messageResId = R.string.rules_no_empty)
    @Bind(R.id.Name)
    EditText etName;

    @NotEmpty(messageResId = R.string.rules_no_empty)
    @Bind(R.id.email)
    EditText etEmail;

    @NotEmpty(messageResId = R.string.rules_no_empty)
    @Bind(R.id.phone)
    EditText etPhone;

    @Bind(R.id.btnSignUp)
    Button btnSignUp;


    public static final String TAG = SignUpFragment.class.getSimpleName();

    private SignUpPresenter presenter;
    SharedPreferences sharedpreferences;
    public static final String MyPREFERENCES = "MyPrefs" ;

    public static void showFragment(BaseActivity sourceActivity) {
        if (!sourceActivity.isFragmentNotNull(TAG)) {
            FragmentTransaction fragmentTransaction = sourceActivity.getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.content_question, new SignUpFragment(), TAG);
            fragmentTransaction.commit();
        }
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_sign_up;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        presenter = new SignUpPresenter(this);
        sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initview();
    }


    @Override
    public void onResume() {
        super.onResume();
    }

    private void initview (){
        btnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!validate()) {
                    onSignupFailed();
                    return;
                } else {
                    presenter.signup();
                }
            }

        });
    }

    @Override
    public void onValidationSucceeded() {
        super.onValidationSucceeded();
        presenter.signup();
    }


    public void onSignupFailed() {
        Toast.makeText(getContext(), "Login failed", Toast.LENGTH_LONG).show();

        btnSignUp.setEnabled(true);
    }


    public boolean validate() {
        boolean valid = true;

        String name = etName.getText().toString();
        String email = etEmail.getText().toString();
        String password = etPhone.getText().toString();

        if (name.isEmpty() || name.length() < 3) {
            etName.setError("at least 3 characters");
            valid = false;
        } else {
            etName.setError(null);
        }

        if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            etEmail.setError("enter a valid email address");
            valid = false;
        }

        if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
            etPhone.setError("between 4 and 10 alphanumeric characters");
            valid = false;
        } else {
            etPhone.setError(null);
        }

        return valid;
    }



    public void gotoQuestionActivity(String email, String name, String phone) {
        QuestionActivity.startActivity((BaseActivity) getActivity(), email, name, phone);
        getActivity().finish();
    }



}

1 个答案:

答案 0 :(得分:0)

您错过了在第二个类中定义共享偏好,就像在第一个类中一样。

sharedpreferences = fragment.getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

将此行放在代码上方

SharedPreferences.Editor editor = sharedpreferences.edit();

在onNext()