Hi i am trying to make a preferences Activity that keeps all the information about a card game and return it. The information arrives from a spinner item selection. The problem arrives when I create a Objet of my class "Configuracion" and I call the method to open SharedPreferences I receive a NullPointerException. What is my problem? thank you!
public class Configuracion extends ActionBarActivity {
private String array_spinner [];
private int numerojugadores;
private int numerobarajas;
private int numerocartas;
private String nombrejugador;
private String numerojug_s = "";
SharedPreferences sp;
SharedPreferences sp1;
private Spinner s;
@Override
public void onBackPressed() {
finish();
return;
}
public void EstablecerConfiguracion(){
// Here's when fails when I create a "Configuracion" Object from other class
// and I call that method
sp1= getSharedPreferences ("config", Context.MODE_PRIVATE);
sp1.getString("NUM_JUG", numerojug_s);
// sp.getString("NUM_BAR", "1");
// sp.getString("NUM_CAR", "3");
// sp.getString("NOM_JUG", "Jugador_Sardi");
switch (sp1.getString("NUM_JUG", numerojug_s)){
case "2":this.numerojugadores = 2;break;
case "3":this.numerojugadores = 3;break;
case "4":this.numerojugadores = 4;break;
case "5":this.numerojugadores = 5;break;
default: this.numerojugadores = 2;System.out.println("ALGO PASA MAL");break;
}
System.out.println(numerojug_s);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configuracion);
array_spinner=new String[4];
array_spinner[0]="2";
array_spinner[1]="3";
array_spinner[2]="4";
array_spinner[3]="5";
s = (Spinner) findViewById(R.id.spinner1);
// s.setSelection(2);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);
s.setOnItemSelectedListener(listener_numjug);
}
protected void onResume(){
super.onResume();
sp = getSharedPreferences("config",Context.MODE_PRIVATE);
sp.getString("NUM_JUG", numerojug_s);
sp.getString("NUM_BAR", "1");
sp.getString("NUM_CAR", "3");
sp.getString("NOM_JUG", "Jugador_Sardi");
switch (sp.getString("NUM_JUG", numerojug_s)){
case "2": s.setSelection(0);break;
case "3": s.setSelection(1);break;
case "4": s.setSelection(2);break;
case "5": s.setSelection(3);break;
default: s.setSelection(0);System.out.println("ALGO PASA MAL");break;
}
System.out.println(numerojug_s);
}
OnItemSelectedListener listener_numjug = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
System.out.println(position + " POSICION");
Editor edit = sp.edit();
switch(position){
case 0: edit.putString("NUM_JUG", "2"); edit.commit();numerojug_s = "2";break;
case 1: edit.putString("NUM_JUG", "3"); edit.commit();numerojug_s = "3";break;
case 2: edit.putString("NUM_JUG", "4"); edit.commit();numerojug_s = "4";break;
case 3: edit.putString("NUM_JUG", "5"); edit.commit();numerojug_s = "5";break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}};
public int getNumJug(){
// sp = getSharedPreferences("config",Context.MODE_PRIVATE);
sp.getString("NUM_JUG", numerojug_s);
return this.numerojugadores;
}
public int getNumBar(){
return this.numerobarajas;
}
public int getNumCar(){
return this.numerocartas;
}
public String getNomJug(){
return this.nombrejugador;
}
public void setNumJug(int numeroJug){
this.numerojugadores = numeroJug;
}
public void setNumBar(int numeroBar){
this.numerobarajas = numeroBar;
}
public void setNumCar(int numeroCar){
this.numerocartas = numeroCar;
}
public void getNomJug(String nombreJug){
this.nombrejugador = nombreJug;
}
}