I want to get a String that is in the strings.xml file, but always gives the following error:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.macave.rastreador/com.macave.rastreador.AjudaActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5345)
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:947)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:555)
at com.macave.rastreador.AjudaActivity.gerarQuestoes(AjudaActivity.java:42)
at com.macave.rastreador.AjudaActivity.<init>(AjudaActivity.java:17)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2410)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5345)
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:947)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)
And this error comes up when I open this Activity:
public class AjudaActivity extends AppCompatActivity {
private List<Questao> listaDeQuestoes = gerarQuestoes();
private ListView lista;
private ArrayAdapter<String> questoes;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela_ajuda);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
lista = (ListView) findViewById(R.id.lista);
final QuestaoAdapter adapter = new QuestaoAdapter(this, listaDeQuestoes);
lista.setAdapter(adapter);
}
public List<Questao> gerarQuestoes() {
//As perguntas e as respectivas respostas
String pergunta1 = this.getResources().getString(R.string.pergunta_1);
String resposta1 = this.getResources().getString(R.string.resposta_1);
// The error occurs because of the two lines above.
List<Questao> questoes = new ArrayList<>();
questoes.add(criaQuestao(pergunta1, resposta1));
return questoes;
}
public Questao criaQuestao(String pergunta, String resposta) {
Questao questao = new Questao(pergunta, resposta);
return questao;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
}
I need to get these Strings from that file for translation purposes. That's no good.