I'm trying to make a small app that takes an input in an EditText number and takes the corresponding value from an array and displays it in a TextView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText e = (EditText) findViewById(R.id.editText1);
final TextView t = (TextView) findViewById(R.id.textView1);
Button b = (Button) findViewById(R.id.button1);
Resources res = this.getResources();
String arr[] = getResources().getStringArray(R.array.example);
final Editable input = e.getText();
final String in2 = input.toString();
int number = Integer.parseInt(in2);
if(number < 0){
t.setText("Input is too small");
} else if (number > 666){
t.setText("Input is too large");
} else {
final String out = arr[number].toString();
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
t.setText(in2);
}
});
}
}
I want it so that if the input is 0 and the button is clicked the TextView is A.
<string-array name="example">
<item >A</item>
<item >B</item>
<item >C</item>
<item >D</item>
</string-array>
Unfortunately whenever I start this up on an emulator or a phone it instantly crashes. Does anyone know what I'm doing wrong?