2

Humm... I can do this easily in Java (and all other languages) but, it doesn't seem to work in Android.

I simply want to get an integer number input from the user, press a button and do some simple math on it, then display it.

I'm using a TextEdit (also tried a TextView) and have tried many different approaches but, none work. The code itself has no errors and I can do the math and display it if using constants.

The problem is in either parsing it to an integer (though, the code-line seems ok, even though it's a tad different than what I use in pure java.

Any help is welcome - I'm sure it's simple and I read many related posts for this, including the Android Dev site - but...

Here's the short complete code:

package com.bt.strint;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class string_integer extends Activity {
    /** Called when the activity is first created. */   
    int aa = 3;
    int y=33;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      final TextView input = (TextView) findViewById(R.id.EditText01);           
      final Button btn = (Button) findViewById(R.id.Button01);
      final TextView showMe = (TextView) findViewById(R.id.TextView01); 

      btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            //showMe.setText("meow");       //ok            
            //showMe.setText(String.valueOf(y * 7)); // ok
            y = Integer.parseInt((String) input.getText());
            showMe.setText(String.valueOf(y));
        }
      });       
    }
}

1 Answer 1

3

You want to use input.getText().toString(), instead of (String) input.getText(); you can't just cast a CharSequence to String.

Sign up to request clarification or add additional context in comments.

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.