I’m writing a small code to convert one amount of currency to another.
The app accepts two values through EditText (configured to accept decimal numbers)- 1) Home or away currency (If one is filled, the field of the other is left blank) 2) The exchange rate.
Here is the code:
package kk.currency;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class CurrencyActivity extends Activity {
/** Called when the activity is first created. */
EditText inpval1;
EditText inpval2;
EditText rate;
int v1 = 0;
int v2 = 0;
int r = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void btnclick(View view) {
try {
inpval1 = (EditText)findViewById(R.id.away);
v1 = Integer.parseInt(inpval1.getText().toString());
inpval2 = (EditText)findViewById(R.id.home);
v2 = Integer.parseInt(inpval2.getText().toString());
rate = (EditText)findViewById(R.id.rate);
r = Integer.parseInt(rate.getText().toString());
}
catch (Exception e) {
Toast.makeText(this, "Empty field", Toast.LENGTH_SHORT).show();
}
finally {
v1 = v2 * r;
EditText destination = (EditText)findViewById(R.id.away);
destination.setText(Integer.toString(v1));
}
}
}
The problem is that upon clicking the button associated with “btnclick” the field which is left blank (and where the answer is supposed to be displayed) displays 0, no matter what input. But when clicked again the correct answer is displayed.
And if I put any value in the field which is supposed to be left blank, the correct value is displayed in the first go.
What could possibly be the problem?!
On removing ‘finally’ and placing it’s content into ‘try’ I don’t even get any response upon clicking of the button.
Also, the LogCat is is completely empty during the execution of the app.
Here is the layout of the app as someone had requested.
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dip"
android:layout_marginLeft="5dip"
android:text="@string/away"
android:textStyle="bold"
android:textSize="20dip"
android:textColor="#4199ae" />
<EditText
android:id="@+id/away"
android:digits="0123456789."
android:inputType="number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:hint="@string/c1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dip"
android:layout_marginLeft="5dip"
android:text="@string/conv"
android:textStyle="bold"
android:textSize="20dip"
android:textColor="#4199ae" />
<EditText
android:id="@+id/rate"
android:digits="0123456789."
android:inputType="number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:hint="@string/rate" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dip"
android:layout_marginLeft="5dip"
android:text="@string/home"
android:textStyle="bold"
android:textSize="20dip"
android:textColor="#4199ae" />
<EditText
android:id="@+id/home"
android:digits="0123456789."
android:inputType="number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:hint="@string/c2" />
<Button
android:id="@+id/but"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dip"
android:text="@string/buttxt"
android:onClick="btnclick" />
My reply from Reddit:
Your issue is because you’re not handling the various possible inputs a user could have.
On the first go, on this line
The value of
returns “”, which cannot be converted to an integer, therefore it is causing an exception. You should be seeing the “Empty field” Toast appearing.
So after that, it gets to the finally step, but because v2 and r and still 0, then it puts 0 in v1, and puts 0 in the away EditText.
Next time you run, it gets 0 from the Away EditText, and works properly.
Do you know how to do debugging? If you’re using Eclipse, doubleclick the line you want to pause at, at the very left, in the grey/blue bar, this will put a little breakpoint icon, and the program will stop there.
So, if you either remove the getting of the value of the EditText at the beginning, it should work. Also, you want to look at implementing some of the other suggestions here, because you are doing some redundant work by getting the pointers to the control views every time you click the button.
Good luck!