0
When seeing the application, I see that in the variable nomedoHumano appears null, and the user’s name should appear.
Just below the method Onclick, recover the name typed. I’ve thought about putting to recover above the String, but it doesn’t work.
Follow the code below:
public class MainActivity extends AppCompatActivity {
private TextView textoNovaFrase;
private Button botaoNovaFrase;
private Preferencias preferencias;
private String nomedoHumano;
private String[] frases = {
nomedoHumano + "se você traçar metas absurdamente altas e falhar, seu fracasso será muito melhor que o sucesso de todos",
"O sucesso normalmente vem para quem está ocupado demais para procurar por ele",
"Se você não está disposto a arriscar, esteja disposto a uma vida comum"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textoNovaFrase = (TextView) findViewById(R.id.textoNovaFraseId);
botaoNovaFrase = (Button) findViewById(R.id.botaoNovaFraseId);
botaoNovaFrase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Preferencias preferencias = new Preferencias(Conversa.this);
nomedoHumano = preferencias.getNomeDoHumano();
Random randomico = new Random();
int numeroAleatorio = randomico.nextInt( frases.length );
textoNovaFrase.setText( frases[ numeroAleatorio ] );
}
});
}
Below the code of mine SharedPreferences:
public class Preferencias {
private Context contexto;
private SharedPreferences preferences;
private final String NOME_ARQUIVO = "ssistente.preferencias";
private final int MODE = 0;
private SharedPreferences.Editor editor;
//Dados assistente
private final String CHAVE_NOMEASS = "nomeDoAssistente";
private final String CHAVE_SEXOMASASS = "seoxMasDoAssistente";
private final String CHAVE_SEXOFEMASS = "seoxFemDoAssistente";
//Dados Humano
private final String CHAVE_NOMEHUM = "nomeDoHumano";
private final String CHAVE_SEXOMASHUM = "sexoMasDoHumano";
private final String CHAVE_SEXOFEMHUM = "sexoFemDoHumano";
private final String CHAVE_SEXOHUM = "sexoEscolhidoHum";
private final String CHAVE_SEXOASS = "sexoEscolhidoAss";
// Como foi seu dia?
private final String CHAVE_DIA = "respostaEscolhida";
public Preferencias( Context contextoParametro){
contexto = contextoParametro;
preferences = contexto.getSharedPreferences(NOME_ARQUIVO, MODE );
editor = preferences.edit();
}
public void salvarDadosNomeeSexo( String nomeDoAssistente, String sexoMasdoAssistente, String sexoFemDoAssistente,
String nomeDoHumano, String sexoMasdoHumano, String sexoFemDoHumano,
String sexoEscolhidoAss, String sexoEscolhidoHum){
editor.putString(CHAVE_NOMEASS, nomeDoAssistente);
editor.putString(CHAVE_SEXOMASASS, sexoMasdoAssistente);
editor.putString(CHAVE_SEXOFEMASS, sexoFemDoAssistente);
editor.putString(CHAVE_NOMEHUM, nomeDoHumano);
editor.putString(CHAVE_SEXOMASHUM, sexoMasdoHumano);
editor.putString(CHAVE_SEXOFEMHUM, sexoFemDoHumano);
editor.putString(CHAVE_SEXOASS, sexoEscolhidoAss);
editor.putString(CHAVE_SEXOHUM, sexoEscolhidoHum);
editor.commit();
}
public void salvarDadosDia(String respostaEscolhida){
editor.putString(CHAVE_DIA, respostaEscolhida);
editor.commit();
}
public HashMap<String, String> getDadosAssHum(){
HashMap<String, String> dadosAssHum = new HashMap<>();
dadosAssHum.put(CHAVE_NOMEASS, preferences.getString(CHAVE_NOMEASS, null));
dadosAssHum.put(CHAVE_SEXOMASASS, preferences.getString(CHAVE_SEXOMASASS, null));
dadosAssHum.put(CHAVE_SEXOFEMASS, preferences.getString(CHAVE_SEXOFEMASS, null));
dadosAssHum.put(CHAVE_NOMEHUM, preferences.getString(CHAVE_NOMEHUM, null));
dadosAssHum.put(CHAVE_SEXOMASASS, preferences.getString(CHAVE_SEXOMASHUM, null));
dadosAssHum.put(CHAVE_SEXOFEMASS, preferences.getString(CHAVE_SEXOFEMHUM, null));
dadosAssHum.put(CHAVE_SEXOASS, preferences.getString(CHAVE_SEXOASS, null));
dadosAssHum.put(CHAVE_SEXOHUM, preferences.getString(CHAVE_SEXOHUM, null));
return dadosAssHum;
}
public String getNomeDoAssistente(){
return preferences.getString(CHAVE_NOMEASS, null);
}
public String getNomeDoHumano(){
return preferences.getString(CHAVE_NOMEHUM, null);
}
public String getRespostaEscolhida(){
return preferences.getString(CHAVE_DIA, null);
}
}
You get
nomedoHumanothroughpreferencias.getNomeDoHumano(), add to question the code of the Preferences class.– ramaral