• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps using an EditText as output

v1nsai

Member
I'm writing a (what was supposed to be) quickie program to flex my unused java muscles and check out android. I'm trying to write a simple celsius to fahrenheit converter, that has 2 TextView labels for F and C, and 2 corresponding EditTexts. When a value is entered in one of the boxes and the convert Button pushed, the empty EditText is filled in with the converted value.

I've had a surprising amount of difficulty accessing my variables from inside the onClick() handler (or any other method) however. I expected to have to convert the EditText value into a string, and then to a double, but in order to reference any of the variables from onClick I've had to label them final, which has prevented me from modifying any of them.

So first of all, why is it necessary for these to be final in the first place? I haven't used Java in a while but I know this wouldn't be a problem in C. And second, how should I write this?
 
It sounds like you are going about it the wrong way, I don't understand what you have now, but I think what you want is something like:

Code:
   private class MyOnClickListener implements OnClickListener {
        public void onClick(View v) {
             EditText input1 = (EditText) findViewById(R.id.inputCell1)
             String s = input1.getText().toString();
             Double d = Double.parseDouble(s);
             Double d2 = d*2;

             EditText output = (EditText) findViewById(R.id.outputCell);
             output.setText(d2.toString());
         }
    }
 
I tried putting all my variable declarations in the onClick definition but I get force closes whenever I put in a value and click the button. Isn't putting variable definitions in a handler really badwrong? I thought that was strictly for instructions, since it will get called multiple times if you click the button more than once, and cause multiple definitions.

It's a really simple project so I'm just gonna post what I've got.

Code:
public class Main extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button b = (Button)this.findViewById(R.id.button);
        b.setOnClickListener( new OnClickListener() 
        {
            public void onClick( View b )
            {
                EditText fartext = (EditText)findViewById(R.id.fartext);
                EditText celtext = (EditText)findViewById(R.id.celtext);
                String farstring = fartext.toString();
                String celstring = celtext.toString();
                double far = Double.parseDouble(farstring);
                double cel = Double.parseDouble(celstring);
                
                if( fartext.toString() != "0" )
                {
                    double celsius = (far - 32 ) / 1.8;
                    celtext.setText( celstring.valueOf(celsius) );
                }
                if( celtext.toString() != "0")
                {
                    double fahrenheit = (cel * 1.8) + 32;
                    fartext.setText( farstring.valueOf(fahrenheit));
                }
            }
        });
    }
}
 
Well I'm guessing that one of the editText boxes is empty and thus when you attempt to do a toString() on the empty box and assign it to a variable it may be freaking out?

Try setting both boxes to 0 initially and see if it fixes it.
 
Well I'm guessing that one of the editText boxes is empty and thus when you attempt to do a toString() on the empty box and assign it to a variable it may be freaking out?

Try setting both boxes to 0 initially and see if it fixes it.

At first I figured that was the problem but with or without zeros I still get force closed. I'm going to have to sit down and figure out debugging with eclipse.
 
I don't think you want to call toString on the edit text anyway, I think you want
Code:
fartext.getText().toString();

also you can see the exception thrown without eclipse debugging by just doing:

adb logcat *:E
 
Hi v1nsai,

I tried out your code, and stepped through it in the debugger.
The problem is in these lines:

Code:
                String farstring = fartext.toString();
                String celstring = celtext.toString();

They don't return the contents of the text fields, as you might expect.
I get these values:

Code:
farstring = android.widget.EditText@43d08e88
celstring = android.widget.EditText@43d0a800

It will then crash when you try to convert those strings to doubles using Double.parseDouble

To retrieve the contents of the text fields, this works:

Code:
                String farstring = fartext.getText().toString();
                String celstring = celtext.getText().toString();

I hope that helps, and gets you going again.

Mark

p.s.

I just noticed that gorn had already answered your question, so my post is redundant.
But I'll leave it here anyway. Next time I'll read the whole thread before I dive in!
 
Awesome thanks a bunch guys, I'm going to take some time and get comfortable with the debugger, I've been putting it off for too long hehe.
 
Back
Top Bottom