1

I'm new to programming and Android. I'm making my best attempt at a simple app and I'm stuck!

I have two editTexts (set to accept numbers only) and a button. The idea is to display the sum of the two user inputs when the button is pressed. However, my app stops working and force closes when I click the button in the emulator.

Any help would be greatly appreciated.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_prevailing_torque);
    Button calculatePrevailing = (Button) findViewById(R.id.button_calculatePrevailing);


    calculatePrevailing.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TextView prevailingSetting = (TextView) findViewById(R.id.textViewSetting);
            EditText editTextPrevailing = (EditText) findViewById(R.id.editTextPrevailing);
            EditText editTextRecommended = (EditText) findViewById(R.id.editTextReccomended);

            int p = Integer.parseInt(editTextPrevailing.getText().toString());
            int r = Integer.parseInt(editTextRecommended.getText().toString());

            prevailingSetting.setText(r+p);
        }
    });
}

I've looked at several similar questions on here but I haven't been able to find anything that I can implement further than what I have already done. But I am probably looking in the wrong places. Thanks!

2
  • 1
    paste the error log here Commented Dec 5, 2012 at 16:27
  • Welcome to SO! What error is it throwing in LogCat? Try: .setText(Integer.toString(r+p)) Commented Dec 5, 2012 at 16:30

3 Answers 3

1

You can't use setText() with an Integer since it expects a CharSequence, you have to use

prevailingSetting.setText(String.valueOf(r+p));

or just

prevailingSetting.setText(""+(r+p));
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! The first bit works great. The second returns 10+20=1020.
yeah you actually have to do the math first, so it would be prevailingSetting.setText(""+(r+p));
0
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_prevailing_torque);
   Button calculatePrevailing = (Button) findViewById(R.id.button_calculatePrevailing);
   TextView prevailingSetting = (TextView) findViewById(R.id.textViewSetting);
   EditText editTextPrevailing = (EditText) findViewById(R.id.editTextPrevailing);
   EditText editTextRecommended =(EditText)findViewById(R.id.editTextReccomended);

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

        int p = Integer.parseInt(editTextPrevailing.getText().toString());
        int r = Integer.parseInt(editTextRecommended.getText().toString());
        int s = r+p;
        prevailingSetting.setText(Integer.toString(s));


    }
    });

Comments

0

Ok ! if this is your problem then first of all convert your editText value to string and after that convert it into integer like !

 String str1=editTextPrevailing.getText().toString();
 String str2=editTextRecommended.getText().toString();
 int p = Integer.parseInt(str1);
  int r = Integer.parseInt(str2);
  int s = r+p;
   prevailingSetting.setText(""+s);

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.