1

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?

1
  • 1
    stacktrace would be really helpful.. Commented Feb 10, 2014 at 17:05

2 Answers 2

1

The problem is that when you start your app, your editText is empty.

So in2 equals to "", and hence you can't parse this value to an Integer.

Move it into your onClick method:

b.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {                
        int number = Integer.parseInt(e.getText().toString());
        if(number < 0){
            t.setText("Input is too small");
        } else if (number >= arr.length){
            t.setText("Input is too large");
        } else {
            t.setText(arr[number]);
        }
    }); 
} 
Sign up to request clarification or add additional context in comments.

Comments

0

Okay just by looking at the code, I don't think it will accomplish what you are trying to achieve!

final Editable input = e.getText();

final String in2 = input.toString();
int number = Integer.parseInt(in2);

This happens in the onCreate method. So when the app runs, it immediately tries to extract some text, but there's nothing to extract so Integer.parseInt(in2) will throw a runtime exception.

This is my suggested workflow, which may involve some re-writing of your code.

In the onCreate method:

  1. Get all the objects you need via findViewById as you have done.

  2. Set listeners to the editText field and the button - the listener for the editText is responsible for tracking any changes that the user types in. In the methods given by the listener, include the following code:

    stringFromEditText = editText.getText().toString();

    Of course you should have declared these earlier on to make that work:

    String stringFromEditText;

    EditText editText;

After that, you can just call an updateTextView() method (which you define on your own). You don't even need a button! Everything is done within the listener's methods (e.g. onTextChanged)

In the updateTextView() method, don't forget to put some logic in that deals with bad inputs or null inputs: when you encounter bad inputs, simply set the value of stringFromEditText to "0". So you would always succeed in parsing it in later -> no more crashes!

I know I haven't been too detailed by providing all the code. But I hope this helps.

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.