problems with SharedPreferences the application stopped!

3

My class: conf.java

public class conf extends AppCompatActivity {
    EditText idplacatxt;
    Button guardar_btn;
    TextView idplacalbl;
    private configuracion conf;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.configuracion);
        idplacatxt = (EditText)findViewById(R.id.txtidplaca);
        guardar_btn=(Button)findViewById(R.id.btnsaveid);
        idplacalbl = (TextView) findViewById(R.id.lblidplaca);

    if(conf.getIDPLACA_S() != null)
        idplacalbl.setText(conf.getIDPLACA_S());

    guardar_btn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            conf.setIDPLACA_S(idplacatxt.getText().toString());
        }
        });
    }
}

My class: config.java

public class configuracion {

private final String SHARED_PREFS_FILE = "HMPrefs";
private final String IDPLACA_S = "IDPLACA";

private Context mContext;

public configuracion(Context context){
    mContext = context;
}

private SharedPreferences getSettings(){
    return mContext.getSharedPreferences(SHARED_PREFS_FILE, 0);
}

public String getIDPLACA_S(){
    return getSettings().getString(IDPLACA_S, null);
}

public void setIDPLACA_S(String IDPLACA){
    SharedPreferences.Editor editor = getSettings().edit();
    editor.putString(IDPLACA_S, IDPLACA);
    editor.commit();
    }
}

layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:background="@color/colorPrimary"
    android:minHeight="?attr/actionBarSize">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="11dp"
        android:layout_marginStart="11dp"
        app:srcCompat="@drawable/logoblancos" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="15dp"
        android:layout_marginStart="15dp"
        android:layout_marginTop="13dp"
        android:layout_toEndOf="@+id/imageView"
        android:layout_toRightOf="@+id/imageView"
        android:text="@string/app_name"
        android:textAppearance="@android:style/TextAppearance.DeviceDefault.Widget.ActionBar.Title.Inverse" />


</android.support.v7.widget.Toolbar>

<TextView
    android:id="@+id/lblidplaca"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/toolbar"
    android:textAppearance="@style/TextAppearance.AppCompat" />

<EditText
    android:id="@+id/txtidplaca"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginTop="200dp"
    android:hint="ID de producto"
    android:inputType="textPersonName" />

<Button
    android:id="@+id/btnsaveid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.03"
    android:text="GUARDAR"
    android:layout_below="@+id/txtidplaca"
    android:layout_alignLeft="@+id/txtidplaca"
    android:layout_alignStart="@+id/txtidplaca"
    android:layout_marginLeft="6dp"
    android:layout_marginStart="6dp"
    android:layout_marginTop="28dp" />

When I try to manipulate Shared preferences the app is closed. For example if obvious

 if(conf.getIDPLACA_S() != null)
        idplacalbl.setText(conf.getIDPLACA_S());

the layout opens but when I try to save the application it closes. If I add that code it does not even open. Personally, I suspect the configuration.java class, what do you think? regards!

Logcat

10-03 10:30:54.157 24330-24330/autanasis.aplicacion E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: autanasis.aplicacion, PID: 24330
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{autanasis.aplicacion/autanasis.aplicacion.conf}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String autanasis.aplicacion.configuracion.getIDPLACA_S()' on a null object reference
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3150)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
                                                                         at android.app.ActivityThread.access$1000(ActivityThread.java:218)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:145)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6934)
                                                                         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:1404)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String autanasis.aplicacion.configuracion.getIDPLACA_S()' on a null object reference
                                                                         at autanasis.aplicacion.conf.onCreate(conf.java:23)
                                                                         at android.app.Activity.performCreate(Activity.java:6609)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3103)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260) 
                                                                         at android.app.ActivityThread.access$1000(ActivityThread.java:218) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:145) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6934) 
                                                                         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:1404) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
    
asked by Yoel Mendoza 03.10.2017 в 16:04
source

2 answers

0

The problem defined in the LogCat is:

  

Caused by: java.lang.NullPointerException: Attempt to invoke virtual   method 'java.lang.String   autanasis.aplicacion.configuracion.getIDPLACA_S () 'on a null object   reference

This is because the value of the configuration instance has a null value at this point, because having a null value and calling the getIDPLACA_S() method generates the problem:

 if(conf.getIDPLACA_S() != null)

the validation for the Configuration instance would be done in this way

 if(conf != null)

But reviewing the code you may notice that you are not initializing conf which causes the problem , initialize it:

public class conf extends AppCompatActivity {
    EditText idplacatxt;
    Button guardar_btn;
    TextView idplacalbl;
    private configuracion conf;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.configuracion);
        idplacatxt = (EditText)findViewById(R.id.txtidplaca);
        guardar_btn=(Button)findViewById(R.id.btnsaveid);
        idplacalbl = (TextView) findViewById(R.id.lblidplaca);

        conf = new Configuracion(getApplicationContext()); //*Inicializa!

    if(conf.getIDPLACA_S() != null)
        idplacalbl.setText(conf.getIDPLACA_S());

    guardar_btn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view) {
            conf.setIDPLACA_S(idplacatxt.getText().toString());
        }
        });
    }
}
    
answered by 03.10.2017 / 17:16
source
0

Modify the class a bit.

public class configuracion {

private final String SHARED_PREFS_FILE = "HMPrefs";
private final String IDPLACA_S = "IDPLACA";

private Context mContext;
SharedPreferences settings;


public configuracion(Context context){
    mContext = context;
    settings = mContext.getSharedPreferences(SHARED_PREFS_FILE, 0);
}



    private String obtener(String clave){
        return settings.getString(clave, "");
    }

    private boolean grabar(String clave,String valor){

    try {

        SharedPreferences.Editor editor = settings.edit();
        editor.putString(clave, valor);
        editor.commit();

            return true;
        }catch(Exception e){
            return false;
        }

    }


    public String getClave(){
        return obtener(IDPLACA);
    }

    public boolean setClave(String IDPLACA) {
        return grabar(IDPLACA_S ,IDPLACA);
    }

}

    
answered by 03.10.2017 в 17:36