Seif Hussam
Seif Hussam

Reputation: 3

Application crashes when setting the text of an EditText

Why does my application always crash whenever I try to change the text of an EditText? I have tried celc = (EditText) findViewById(R.id.cel) ; far = (EditText) findViewById(R.id.fa) ; Ran = (EditText) findViewById(R.id.ran) ; kelvin = (EditText) findViewById(R.id.kev) ;
celc.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            String value = s.toString() ;
            double c1 = Double.parseDouble(value) ; 
            double f1 = (32+((9.0/5)*c1));
            double r1 = f1+460 ; 
            double k1 = c1 + 273.0 ;

            far.setText(f1+"");
            Ran.setText((r1 + "")); 
            kelvin.setText(k1+""); 

        }
    }); but it doesn't work.

Upvotes: 0

Views: 1526

Answers (2)

Vaibhav Barad
Vaibhav Barad

Reputation: 625

  1. Declare the EditText in the xml file
  2. Find the EditText in the activity
  3. Set the text in the EditText

And If you check the docs for EditText, you'll find a setText() method. It takes in a String and a TextView.BufferType. For example:

EditText editText =     (EditText)findViewById(R.id.edit_text);
editText.setText("Google is your friend.", TextView.BufferType.EDITABLE); 

Or Use +, the string concatenation operator:

 ed = (EditText) findViewById (R.id.box);
    int x = 10;
    ed.setText(""+x);

or

String.valueOf(int):
ed.setText(String.valueOf(x))

Upvotes: 3

Maybe you dont have linked your view object with your java object.

You made this with findViewById method.

your layout file:

<EditText ... android:id="@+id/edittext" />

Java Code: on class definition:

private EditText myEditText;

and on method onCreate:

        edittext = (EditText) findViewById(R.id.editText);

Best regards!

Upvotes: 0

Related Questions