我是 Java 的初学者,但我正在尝试构建练习,但遇到了这个问题。用户输入捐款,按下按钮,捐款的值(value)就会增加。我编写了整个代码,但是当我单击按钮添加到捐赠计数时,应用程序崩溃了。我遵循了我老师的所有教诲。我在 Activity 中创建了一个方法,在 ui 构建器中,我转到单击属性上的按钮并将其设置为该方法。它应该进行得很顺利,但每当我点击按钮时它就会崩溃。这是我将 onClick 属性设置为 computeDonation 的 Activity 。
public void computeDonation(View v){
String addedText = ((EditText)findViewById(R.id.AmountText)).getText().toString();
DonationModel donation = new DonationModel(addedText);
String answer = donation.calcDonation();
((TextView)findViewById(R.id.totalText)).setText(answer);
}
这是模型:
String textAdded;
double addedDonation;
public DonationModel(String y) {
textAdded = y;
}
public String calcDonation(){
double counter = Double.parseDouble(textAdded);
addedDonation += counter;
Locale locale = new Locale("en", "CA");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
return fmt.format(addedDonation);
}
我什至尝试过不使用数字格式,只是为了看看它是否会发回 Activity ,但事实并非如此。顺便说一句,我在类里面使用的是旧版本的 android studio。
这是日志:
02-15 17:12:28.797 21619-21619/ca.roumani.donations E/AndroidRuntime: FATAL EXCEPTION: main Process: ca.roumani.donations, PID: 21619 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:5197) at android.view.View$PerformClick.run(View.java:20909) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5197) at android.view.View$PerformClick.run(View.java:20909) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) Caused by: java.lang.NumberFormatException: Invalid double: "" at java.lang.StringToReal.invalidReal(StringToReal.java:63) at java.lang.StringToReal.parseDouble(StringToReal.java:267) at java.lang.Double.parseDouble(Double.java:301) at ca.roumani.donations.DonationModel.calcDonation(DonationModel.java:20) at ca.roumani.donations.DonationActivity.computeDonation(DonationActivity.java:24) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5197) at android.view.View$PerformClick.run(View.java:20909) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
最佳答案
您的 textAdded 似乎是一个空字符串,这会导致 NumberFormatException 并使您的应用程序崩溃。你可以做些什么来防止这种情况,是在解析之前检查你的 textAdded 是否不为空:
public String calcDonation() {
if (TextUtils.isEmpty(textAdded)) {
//textAdded is empty, so just return an empty String
return "";
}
//We're sure that textAdded has a value, so we can try to parse it
double counter = Double.parseDouble(textAdded);
addedDonation += counter;
Locale locale = new Locale("en", "CA");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
return fmt.format(addedDonation);
}
或者只是阻止在用户在 EditText View 中输入值之前调用该方法:
public void computeDonation(View v){
String addedText = ((EditText)findViewById(R.id.AmountText)).getText().toString();
if (TextUtils.isEmpty(textAdded)) {
Toast.makeText(context, "Please enter a valid number", Toast.LENGTH_SHORT).show();
} else {
DonationModel donation = new DonationModel(addedText);
String answer = donation.calcDonation();
((TextView)findViewById(R.id.totalText)).setText(answer);
}
}
关于java - 单击按钮后,Android Studio 应用程序不断崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42261289/