0

This is my following java code, question is the xml id. For some reason I keep on getting null whenever I try to print it out, is something wrong?

public class MainActivity extends AppCompatActivity{

private boolean correct;
private String questionTxt;

public int correctAnswer()
{
    int first = (int)(Math.random() * 10);
    int second = (int)(Math.random() * 10);
    int answer = first + second;
    questionTxt = first + " + " + second;
    return answer;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

    TextView questionText = (TextView)findViewById(R.id.question);
    questionText.setText(questionTxt + "");
}

xml code, just regular textview:

 <TextView
    android:id="@+id/question"
    android:background="@color/colorAccent"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:layout_above="@+id/option1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="11dp"
    android:screenOrientation="portrait"/>
17
  • Show your xml codes pls Commented Dec 29, 2016 at 6:26
  • 5
    you are not calling correctAnswer() from anywhere, so your questionTxt is null Commented Dec 29, 2016 at 6:26
  • post full code here Commented Dec 29, 2016 at 6:27
  • 1
    Your problem isn't the XML Commented Dec 29, 2016 at 6:27
  • where do I call correctAnswer? Commented Dec 29, 2016 at 6:29

2 Answers 2

1

call this method correctAnswer(); before setting value into textview

public class MainActivity extends AppCompatActivity{

private boolean correct;
private String questionTxt;

public int correctAnswer()
{   
int first = (int)(Math.random() * 10);
int second = (int)(Math.random() * 10);
int answer = first + second;
questionTxt = first + " + " + second;
return answer;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

correctAnswer();
TextView questionText = (TextView)findViewById(R.id.question);
questionText.setText(questionTxt + "");
}
Sign up to request clarification or add additional context in comments.

2 Comments

@LarryJing if you got the answer vote for this question it may useful to someone
I have to wait one minute to accept it and my vote doesn't count publicly since I only have 10 reputation XD
0

You should move the method after onCreate

   public class MainActivity extends AppCompatActivity{

    private boolean correct;
    private String questionTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
        TextView questionText = (TextView)findViewById(R.id.question);
        int answer = correctAnswer();
        questionText.setText(answer+");

    }

    public int correctAnswer()
    {
        int first = (int)(Math.random() * 10);
        int second = (int)(Math.random() * 10);
        int answer = first + second;
        questionTxt = first + " + " + second;
        return answer;
    }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.