Problem with global variable in Android

-1

Variables_HassMovil

public class Variables_HassMovil{
private static Variables_HassMovil instance;

private String cuenta,calibre,nombrecalibre,fechamostrar;

private Variables_HassMovil(){}

public void setCuenta(String Cuenta){
    this.cuenta=Cuenta;
}
public String getCuenta(){
    return this.cuenta;
}

public String getCalibre() {
    return calibre;
}

public void setCalibre(String calibre) {
    this.calibre = calibre;
}

public String getNombrecalibre() {
    return nombrecalibre;
}

public void setNombrecalibre(String nombrecalibre) {
    this.nombrecalibre = nombrecalibre;
}

public String getFechamostrar() {
    return fechamostrar;
}

public void setFechamostrar(String fechamostrar) {
    this.fechamostrar = fechamostrar;
}

public static synchronized  Variables_HassMovil getInstance(){
    if (instance==null){
        instance=new Variables_HassMovil();
    }
    return instance;
}

}

VariablesOpcion_HassMovil

public class VariablesOpcion_HassMovil extends Application {

private String cuenta=null;
private String calibre=null;
private String calibrenombre=null;
private String fechamostrar=null;



public String getCalibre() {
    return calibre;
}

public void setCalibre(String calibre) {
    this.calibre = calibre;
}

public String getCalibrenombre() {
    return calibrenombre;
}

public void setCalibrenombre(String calibrenombre) {
    this.calibrenombre = calibrenombre;
}

public String getCuenta(){
    return this.cuenta;
}

public void setCuenta(String Cuenta){
    this.cuenta=Cuenta;
}

public String getFechamostrar() {
    return fechamostrar;
}

public void setFechamostrar(String fechamostrar) {
    this.fechamostrar = fechamostrar;
}

}

I want to simplify the following part:

VariablesOpcion_HassMovil variables = (VariablesOpcion_HassMovil) getApplication();
 Log.d("VARIABLE", variables.getFechamostrar());

How can I initialize it from the Oncreate because it does not leave me mark error.It's not like putting TextView x; and being able to use it for all the activity, how do I do it?

    
asked by DoubleM 28.01.2017 в 12:11
source

1 answer

2

If it is a global variable, you have to declare it as static.

Go to your class Application or Activity principal, declare the variables as static , for example:

public static Variables_HassMovil misVariables;

... and then you can access that information from anywhere in the following way.

ActivityMain.misVariables.getblablabla();
    
answered by 29.01.2017 в 06:21