作者:用户r8l5835vd6 | 来源:互联网 | 2023-02-11 17:59
我是java的初学者,但我正在尝试构建练习,我遇到了这个问题.用户键入捐赠,按下按钮并且捐赠的值递增.我写了整个代码但是当我点击按钮添加到捐赠计数时,应用程序崩溃了.我遵循了老师的所有教导.我在活动中创建了一个方法,在ui构建器中,我转到click属性上的按钮并将其设置为该方法.它应该顺利进行,但每当我点击按钮时它就会崩溃.这是我将onClick属性设置为computeDonation的活动.
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);
}
我甚至试过没有数字格式只是为了看它是否发送回活动,但它没有.我的班级BTW我正在使用旧版本的android studio.
这是logcat:02-15 17:12:28.797 21619-21619/ca.roumani.donations E/AndroidRuntime:FATAL EXCEPTION:main进程:ca.roumani.donations,PID:21619 java.lang.IllegalStateException:无法执行android的方法:在android.view.View $ PerformClick上的android.view.View.performClick(View.java:5197)的android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)上点击.运行(View.java:20909)android.os.Handler.handleCallback(Handler.java:739)android.os.Handler.dispatchMessage(Handler.java:95)android.os.Looper.loop(Looper.java) :145)在java.lang.reflect.Method.invoke(Method.java:372)的java.lang.reflect.Method.invoke(Native Method)的android.app.ActivityThread.main(ActivityThread.java:5942)上com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)引起:java.lang.reflect.InvocationTargetException at at java.lang.reflect.Method.inv 位于android.view.View的android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)的java.lang.reflect.Method.invoke(Method.java:372)上的oke(Native Method).在Android.os.Handler.dispatchMessage(Handler)上的android.view.View $ PerformClick.run(View.java:20909)android.os.Handler.handleCallback(Handler.java:739)执行点击(View.java:5197) .java:95)在android.app.Looper.loop(Looper.java:145)的android.app.ActivityThread.main(ActivityThread.java:5942)at java.lang.reflect.Method.invoke(Native Method)at 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)引起:java.lang.NumberFormatException:在java.lang.StringToReal.parseDouble(StringToReal.java:267)的java.lang.StringToReal.invalidReal(StringToReal.java:63)中无效的double:""在ca.roumani的java.lang.Double.parseDouble(Double.java:301).donations.DonationModel.calcDonation(DonationModel.java:20)位于java.lang.reflect的java.lang.reflect.Method.invoke(Native Method)中的ca.roumani.donations.DonationActivity.computeDonation(DonationActivity.java:24).在android.view.View.performClick(View.java:5197)的android.view上的android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)上的Method.invoke(Method.java:372) .在android.os.Handler.dispatchMessage(Handler.java:95)的android.os.Looper上的android.os.Handler.handleCallback(Handler.java:739)上查看$ PerformClick.run(View.java:20909).在java.lang.reflect.Method.invoke的java.lang.reflect.Method.invoke(Native Method)的android.app.ActivityThread.main(ActivityThread.java:5942)上循环(Looper.java:145)(方法. java:372)来自com.android.internal.os.ZygoteInit.main(.)
1> Hookah_Smoka..:
看起来你的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视图中输入值之前调用该方法:
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);
}
}