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?