0

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.
  • 5
    How the heck are we supposed to know with neither code no stack trace? So I'll go with "the gremlins did it". – Gabe Sechan Sep 25 '15 at 22:54
  • Please provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) demonstrating your problem. In this case, please post [the Java stack trace associated with your crash](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this), along with the code referenced in the stack trace. – CommonsWare Sep 25 '15 at 23:04
  • put your logcat error. – Deepak Goyal Sep 25 '15 at 23:09
  • Android RunTime @DeepakGoyal – Seif Hussam Sep 26 '15 at 10:54

2 Answers2

3
  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))
Vaibhav Barad
  • 625
  • 8
  • 17
0

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!