I am creating an app which divides one number by another number. I have tried to add code to display a dialog when there is an empty edit text, but the app forcecloses. Here is my code.
public class KdCalculator extends Activity implements OnClickListener{
EditText kills;
EditText deaths;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kd_activity);
Button calculate = (Button)findViewById(R.id.ButtonCalculate);
calculate.setOnClickListener(this);
kills = (EditText)findViewById(R.id.EditTextKills);
deaths = (EditText)findViewById(R.id.EditTextDeaths);
}
public void onClick(View paramView)
{
boolean invalid = false;
String str1 = kills.getText().toString();
String str2 = deaths.getText().toString();
if (str1.equals("") || str2.equals(""))
{
new AlertDialog.Builder(this)
.setTitle("No Number")
.setMessage("Please enter a number for both kills and deaths!")
.setNeutralButton("Ok", null)
.show();
invalid = true;
}
double d1 = Double.parseDouble(str2);
double d2 = Double.parseDouble(str1);
if ((d1 == 0.0D) || (d2 == 0.0D))
{
new AlertDialog.Builder(this)
.setTitle("Invalid Number")
.setMessage("Please enter a number different than 0 for both kills and deaths!")
.setNeutralButton("Ok", null)
.show();
invalid = true;
}
double d3 = d2 / d1;
DecimalFormat localDecimalFormat = new DecimalFormat("#.###");
String str3 = "Your K/D Ratio is : " + localDecimalFormat.format(d3);
if(!invalid){
new AlertDialog.Builder(this)
.setTitle("K/D Ratio")
.setMessage(str3)
.setNeutralButton("Ok", null)
.show();
}
invalid = false;
}
}
the logcat error is:
05-27 11:43:53.327: W/dalvikvm(6090): threadid=1: thread exiting with uncaught exception (group=0x40020b80)
05-27 11:43:53.357: E/AndroidRuntime(6090): FATAL EXCEPTION: main
05-27 11:43:53.357: E/AndroidRuntime(6090): java.lang.NumberFormatException:
05-27 11:43:53.357: E/AndroidRuntime(6090): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
05-27 11:43:53.357: E/AndroidRuntime(6090): at java.lang.Double.parseDouble(Double.java:287)
05-27 11:43:53.357: E/AndroidRuntime(6090): at com.blackops2.KdCalculator.onClick(KdCalculator.java:53)
05-27 11:43:53.357: E/AndroidRuntime(6090): at android.view.View.performClick(View.java:2411)
05-27 11:43:53.357: E/AndroidRuntime(6090): at android.view.View$PerformClick.run(View.java:8819)
05-27 11:43:53.357: E/AndroidRuntime(6090): at android.os.Handler.handleCallback(Handler.java:587)
05-27 11:43:53.357: E/AndroidRuntime(6090): at android.os.Handler.dispatchMessage(Handler.java:92)
05-27 11:43:53.357: E/AndroidRuntime(6090): at android.os.Looper.loop(Looper.java:123)
05-27 11:43:53.357: E/AndroidRuntime(6090): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-27 11:43:53.357: E/AndroidRuntime(6090): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 11:43:53.357: E/AndroidRuntime(6090): at java.lang.reflect.Method.invoke(Method.java:521)
05-27 11:43:53.357: E/AndroidRuntime(6090): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-27 11:43:53.357: E/AndroidRuntime(6090): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-27 11:43:53.357: E/AndroidRuntime(6090): at dalvik.system.NativeStart.main(Native Method)
Even if your
EditTextare empty you’re still parsing the emptyStringsfrom thoseEditTextas your code continues to be executed after you show theAlertDialog, instead you should return from theonClickmethod to stop any calculations(when theEditTextare empty or aren’t filled):