即使代码正确,共享首选项也会显示NullPointerException

时间:2016-06-14 05:54:07

标签: android android-studio

我使用SharedPreferences进行简单的登录和注销会话。但是,我收到了这个错误:

  

致命的例外:主要                                                                                   过程:com.chayan.jecrcuniversity,PID:16940                                                                                   java.lang.NullPointerException:尝试调用接口方法   ' android.content.SharedPreferences $编辑器   android.content.SharedPreferences.edit()'在null对象引用上                                                                                       在   com.chayan.jecrcuniversity.StudentLogin $ 1.onClick(StudentLogin.java:40)

这是我的活动代码:

public class StudentLogin extends AppCompatActivity {

Button submit;
EditText un,pass;

public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Username = "unameKey";
public static final String Password = "passKey";
//public static final String Email = "emailKey";
SharedPreferences sharedpreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    un=(EditText)findViewById(R.id.editText4);
    pass=(EditText)findViewById(R.id.editText6);
    submit=(Button)findViewById(R.id.button6);

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String username=un.getText().toString();
            String password=pass.getText().toString();

            SharedPreferences.Editor editor = sharedpreferences.edit();

            editor.putString(Username, username);
            editor.putString(Password, password);
            editor.apply();

            startActivity(new Intent(StudentLogin.this,MainActivity.class));
                Toast.makeText(StudentLogin.this,"Welcome, "+un.getText().toString(),Toast.LENGTH_LONG).show();

        }
    });
}
}

2 个答案:

答案 0 :(得分:0)

如果您遇到上述方法的问题,请尝试使用PreferenceManager类。

在SharedPreferences中保存名称:

SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        Editor editor = sharedPreferences.edit();
        editor.putString(key, name);
        editor.apply();

从SharedPreferences获取名称:

SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        String name = sharedPreferences.getString(key, "default value");

答案 1 :(得分:0)

使用这两种方法来保存共享偏好并从中获取价值。

UIImageJPEGRepresentation

要从func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { photoImgView.image=info[UIImagePickerControllerOriginalImage] as? UIImage imageData = UIImageJPEGRepresentation((info[UIImagePickerControllerOriginalImage] as? UIImage)!, 0.5) print(imageData) picker .dismissViewControllerAnimated(true, completion: nil) } 获取价值,请使用此方法:

public void savePreferences(Context context,
                                       String key, String value) {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

编辑:使用方法:

要节省价值,请使用以下行:

SharedPreferences

要获得价值,请使用以下行:

public String getPreferences(Context context, String prefKey) {
    SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(context);
    return sharedPreferences.getString(prefKey, "");
}