0

I get the text

"Error:(45, 37) error: non-static method getText() cannot be referenced from a static context"

Did I use ClassName instead of variable name? I want to create an EditText for my quiz where the user has to put in the answer, how do I do that?

Thank you for your help

package com.example.android.myquizapp2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

  public void getResults(View view) {
    /*
    Code for answers
    */

    boolean answerArray[];
    answerArray = new boolean[10];
    RadioButton answer = (RadioButton) findViewById(R.id.answerOne);
    answerArray[0] = answer.isChecked();
    answer = (RadioButton) findViewById(R.id.question22group);
    answerArray[1] = answer.isChecked();
    answer = (RadioButton) findViewById(R.id.question13group);
    answerArray[2] = answer.isChecked();
    answer = (RadioButton) findViewById(R.id.question14group);
    answerArray[4] = answer.isChecked();

    CheckBox answerCheckBox = (CheckBox) findViewById(R.id.taipei);
    answerArray[5] = answerCheckBox.isChecked();

    EditText answerEditText = (EditText) findViewById(R.id.question01group);
    answerArray[6] = answerEditText.isTextSelectable();

    This is where I have my mistake(I get the text
                                    "Error:(45, 37) error: non-static method getText() cannot be referenced from a static context")

    String UserAnswer = EditText.getText().trim();

    Boolean truth = UserAnswer.equals(answerEditText);

    /*
    Score
    */

    int score = calculateScore(answerArray);

    displayScore(calculateScore(answerArray));

    if (score == 5)
      score = 'A';
    if (score == 4)
      score = 'B';
    else if (score == 3)
      score = 'C';
    else if (score == 2)
      score = 'D';
    else if (score == 1)
      score = 'E';
    else
      score = 'F';

    System.out.println("Score: " + score + " score: " + score);
  }

  private int calculateScore(boolean scoreArray[]) {
    int Score = 0;
    for (int i = 0; i < 10; i++) {
      if (scoreArray[i]) {
        Score = Score + 1;
      }
    }
    return Score;
  }

  /*
  I wrote return Score instead of methodScore
  */

  /*
  Method score on screen
  */

  private void displayScore(int finalScore) {
    Toast scoreMessage = Toast.makeText(this, "Good job!" + finalScore, Toast.LENGTH_SHORT);
    scoreMessage.show();
  }

  /*
  Method which will show the correct answers
  */

  public void correctAnswers(View view) {

    ScrollView scrollToTop = (ScrollView) findViewById(R.id.mainScrollView);
    scrollToTop.fullScroll(ScrollView.FOCUS_UP);

    RadioGroup choiceGroup = (RadioGroup) findViewById(R.id.question1group);
    choiceGroup.check(R.id.answerOne);
    choiceGroup = (RadioGroup) findViewById(R.id.question2group);
    choiceGroup.check(R.id.question22group);
    choiceGroup = (RadioGroup) findViewById(R.id.question3group);
    choiceGroup.check(R.id.question13group);
    choiceGroup = (RadioGroup) findViewById(R.id.question4group);
    choiceGroup.check(R.id.question14group);

    Toast infoMessage =
        Toast.makeText(this, "These are the correct answers for the quiz.", Toast.LENGTH_LONG);
    infoMessage.show();
  }
}
Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
julig
  • 11
  • Possible duplicate of [What does the 'static' keyword do in a class?](https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-a-class) – sɐunıɔןɐqɐp Aug 19 '18 at 18:36

1 Answers1

0

you need to take EditText object like answerEditText.getText().toString().trim(); instead of calling the class name