androidguy
androidguy

Reputation: 75

Can't get result from three EditText's?

I am trying to take numbers from three EditText boxes that the user fills in, then through some simple math and the user pressing a button, there needs to be an answer through a textView. Also whenever I launch this app on the emulator it says that it needs to Force close once I click on the button to go to this layout and code. Here is the code:

                 package com.app.one;

import com.app.one.R;

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

public class monthlyp extends Activity {
int x = 10;
int y= 10;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.monthlyp);

      final TextView input = (TextView) findViewById(R.id.editone);           
      final Button btn = (Button) findViewById(R.id.equals);
      final EditText textView13 = (EditText) findViewById(R.id.textView13); 

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


            y = Integer.parseInt(input.getText().toString());
            textView13.setText(String.valueOf(y));
        }
      });       
}
    }

I know it only has one EditText area but that's because I need help adding two more and being able to use them as integers to add/subtract/divide/multiply. The xml has all three edittext boxes and an equals button that will solve what is entered. Thanks for the help and please ask me if you need any more information.

Upvotes: 0

Views: 189

Answers (4)

dten
dten

Reputation: 2374

You say that input is a textview

final TextView input = (TextView) findViewById(R.id.editone);    

then you got the integer value of that textview

y = Integer.parseInt(input.getText().toString());

I suggest that's not what you intended

Upvotes: 0

Thomas Dignan
Thomas Dignan

Reputation: 7102

Two things right away:

  1. You converted the string to an integer, and then back to an integer again to put it in the other field. This is bad, it's called yo-yo code. Save the string value and store that, instead.

  2. Integer.parseInt() can throw a NumberFormatException if you don't put a valid integer in that EditText. You need to try/catch around it for that exception and handle the invalid cases, preferably by calling setError() on your edit text.

http://developer.android.com/reference/java/lang/Integer.html#parseInt%28java.lang.String%29

edit: and yes, like the others said.. don't name your EditText "textView13"

Upvotes: 1

Earl
Earl

Reputation: 811

You appear to be getting your input from the TextView and posting the output to the EditText. Shouldn't that be the other way around?

Upvotes: 0

Ashterothi
Ashterothi

Reputation: 3282

First off it appears that your input is a TextView and your output is an EditText. This seems backwards. If your XML is correct and your input is an EditText you may have problems trying to cast it as a TextView.

Upvotes: 2

Related Questions