Im not sure why im getting NullPointerException. The method getQuestion() currently points to a hard coded String for troubleshooting purposes. Im trying to generate a different string before setContentView() runs to avoid having to refresh the UI.
First I call the method getQuestion()
TextView t=new TextView(this);
t=(TextView)findViewById(R.id.textView4);
try {
//above executes well :) ... problem is below
t.setText(quiz.getQuestion());
} catch (Exception e1) {
// TODO Auto-generated catch block
Toast.makeText(AlarmReceiverActivity.this, "getQuestion() failed", Toast.LENGTH_LONG).show();
e1.printStackTrace();
}
setContentView(R.layout.main);
This is some of my Question.class:
Question(){
question="null";
answer="null";
try {
newQuestion();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//Question constructor
String getQuestion(){
return "This is a question?";
}//get question
you cannot call
t=(TextView)findViewById(R.id.textView4);before callingsetContentView(R.layout.main);. First you need to set your layout, then findViewById can find the view from your defined layout