2

So I am making a little app where it takes a comma separated list and returns a random word in that list and I am having trouble getting the text from the EditText view.

I saw on Stackoverflow that you're suppose to use the EditText.getText().toString() methods in order to get the text from the EditText. However, when I use those method, the app stops working. I ran the debugger and my Input variable refers to an empty string and I also get an error that there is no local submit variable.

This is the java file:

public class MainActivity extends AppCompatActivity { EditText UserInput; Button reset; Button Submit; TextView result; String Input; String Result; String word; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); UserInput = (EditText) findViewById(R.id.UserInput); reset = (Button) findViewById(R.id.Reset); Submit = (Button) findViewById(R.id.Choose); result = (TextView) findViewById(R.id.Result); Input = UserInput.getText().toString(); Result = "Your choice should be:"; Submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Random random = new Random(); String[] List = Input.split(","); int num = random.nextInt(List.length - 1); word = List[num]; Result = Result + word; result.setText(Result); } }); } 

This is the EditText view of the activity:

<EditText android:id="@+id/UserInput" android:layout_width="273dp" android:layout_height="59dp" android:layout_marginStart="8dp" android:layout_marginTop="36dp" android:layout_marginEnd="8dp" android:ems="10" android:hint="Choice A, Choice B, Choice C,..." android:inputType="text" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.515" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/Instructions" /> 

For example, if I write in the EditText 'a,b', I expect Input to be 'a,b' but it is not. When I was debugging, it showed me it was an empty string and I do not know why. Also. I got another error that there is no local submit variable in the onClick method. How do I fix these errors?

1
  • 1
    Post logcat error. Commented Jan 1, 2019 at 17:59

3 Answers 3

3

You are getting the text from EditText at the time of declaring because of which Input is null. Input is not watching the edittext for change in its content.

To do this, you have to get Input again, just before submitting.

Submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Random random = new Random(); Input = UserInput.getText().toString(); if (Input != null) { String[] List = Input.split(","); int num = random.nextInt(List.length - 1); word = List[num]; Result = Result + word; result.setText(Result); } } }); 
Sign up to request clarification or add additional context in comments.

1 Comment

Side note suggestion, in JAVA it's preferable to start variable names with small letters. To be precise camelCase
2

Move Input = UserInput.getText().toString(); inside the onClick method.

Submit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Input = UserInput.getText().toString(); Random random = new Random(); String[] List = Input.split(","); int num = random.nextInt(List.length - 1); word = List[num]; Result = Result + word; result.setText(Result); } } 

You need to read the EditText only when the button is clicked, not when the app first starts.

Layman's explanation: In your current code, you read the EditText when the app is launched. Since it is empty at the time, you get an empty string when you read it.

Comments

0

Debugger shows empty string in such case, num may become 0, which then can cause IndexOutOfBounds Exception.

String[] List = Input.split(","); int num = random.nextInt(List.length - 1); word = List[num]; 

Post your logcat for further assistance.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.