Error getting SharedPreferences

1

I'm trying to create a class to get the predefined data, but when I call the method Load() the app stops. Could you help me? Thanks

package com.example.nano.prueba.login.loginprueba;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;


/**
 * Created by admin on 02/04/2016.
 */
public class LoadPreferencias extends AppCompatActivity {

    private String User;
    private String Pass;

    public void Load(){

        SharedPreferences Preferencias = getSharedPreferences("DatosUser", Context.MODE_PRIVATE);
        User =(Preferencias.getString("User",""));
        Pass = (Preferencias.getString("Pass", ""));

    }

    public String GetUser()
    {
        Load();
        return User;
    }
    public String GetPass()
    {
        return Pass;
    }



}

I add error code

  

04-03 09: 55: 44.445 31786-31786 /? E / AndroidRuntime: FATAL EXCEPTION:   main                                                      Process: com.example.nano.prueba.login.logintest, PID: 31786                                                      java.lang.RuntimeException: Unable to start activity   ComponentInfo {com.example.nano.test.login.logintest / com.example.nano.test.login.logintest.MainActivity}:   java.lang.NullPointerException: Attempt to invoke virtual method   'android.content.SharedPreferences   android.content.Context.getSharedPreferences (java.lang.String, int) '   on a null object reference                                                          at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2658)                                                          at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2723)                                                          at android.app.ActivityThread.access $ 900 (ActivityThread.java:172)                                                          at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1422)                                                          at android.os.Handler.dispatchMessage (Handler.java:102)                                                          at android.os.Looper.loop (Looper.java:145)                                                          at android.app.ActivityThread.main (ActivityThread.java:5832)                                                          at java.lang.reflect.Method.invoke (Native Method)                                                          at java.lang.reflect.Method.invoke (Method.java:372)                                                          at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1399)                                                          at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1194)                                                       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method   'android.content.SharedPreferences   android.content.Context.getSharedPreferences (java.lang.String, int) '   on a null object reference                                                          at android.content.ContextWrapper.getSharedPreferences (ContextWrapper.java:184)                                                          at com.example.nano.test.login.logintest.LoadPref.Load (LoadPref.java:16)                                                          at com.example.nano.test.login.logintest.MainActivity.onCreate (MainActivity.java:41)                                                          at android.app.Activity.performCreate (Activity.java:6221)                                                          at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1119)                                                          at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2611)                                                          at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2723)                                                          at android.app.ActivityThread.access $ 900 (ActivityThread.java:172)                                                          at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1422)                                                          at android.os.Handler.dispatchMessage (Handler.java:102)                                                          at android.os.Looper.loop (Looper.java:145)                                                          at android.app.ActivityThread.main (ActivityThread.java:5832)                                                          at java.lang.reflect.Method.invoke (Native Method)                                                          at java.lang.reflect.Method.invoke (Method.java:372)                                                          at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1399)                                                          at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1194)

    
asked by Alejandro Echeverria 03.04.2016 в 01:40
source

1 answer

5

According to your error message:

  

android.content.Context.getSharedPreferences   ComponentInfo {com.example.nano.test.login.logintest / com.example.nano.test.login.logintest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content. Context.getSharedPreferences

You are calling the Load () method from MainActivity, the Load () method needs to receive the Context, so the error! Modify your method to receive it:

   public void Load(Context context){

    SharedPreferences Preferencias = context.getSharedPreferences("DatosUser", Context.MODE_PRIVATE);
    User =(Preferencias.getString("User",""));
    Pass = (Preferencias.getString("Pass", ""));

}

Although a better design would be to create 2 methods that you call when you need the User or Pass values, your class LoadPreferencias does not need to extend AppCompatActivity , and here you can add the methods to save the values, setUser() and setPass() :

public class LoadPreferencias {

    public static String GetUser(Context context)
    {
     SharedPreferences Preferencias = context.getSharedPreferences("DatosUser", Context.MODE_PRIVATE);
    String   User =Preferencias.getString("User","");
        return User;
    }


    public static String GetPass(Context context)
    {
      SharedPreferences Preferencias = context.getSharedPreferences("DatosUser", Context.MODE_PRIVATE);
    String   Pass =Preferencias.getString("Pass","");
        return Pass;
    }  
} 

From MainActivity you would choose the methods in this way:

String usuario = LoadPreferencias.GetUser(getApplicationContext());
String password = LoadPreferencias.GetPass(getApplicationContext());
    
answered by 03.04.2016 / 03:59
source