gpt4 book ai didi

java - 安卓初学者 : Empty Edit Text

转载 作者:行者123 更新时间:2023-11-29 03:46:11 25 4
gpt4 key购买 nike

我正在创建一个将一个数字除以另一个数字的应用程序。我尝试添加代码以在编辑文本为空时显示对话框,但应用程序强制关闭。这是我的代码。

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;
}


}

logcat 错误是:

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)

最佳答案

即使您的 EditText 是空的,您仍然会从那些 EditText 中解析空的 Strings,因为您的代码会在您之后继续执行显示 AlertDialog,您应该从 onClick 方法返回以停止任何计算(当 EditText 为空或未填充时):

public void onClick(View paramView) { 
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();
return;
}
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();
return;
}
double d3 = d2 / d1;
DecimalFormat localDecimalFormat = new DecimalFormat("#.###");
String str3 = "Your K/D Ratio is : " + localDecimalFormat.format(d3);

new AlertDialog.Builder(this)
.setTitle("K/D Ratio")
.setMessage(str3)
.setNeutralButton("Ok", null)
.show();
}

关于java - 安卓初学者 : Empty Edit Text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10775222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com