简体   繁体   中英

Android Studio app keeps crashing after button click

Im a beginner in java but I'm trying to build exercises and I ran into this problem. The user types in a donation, presses the button and the value of donations increments. Ive written the entire code but when I click the button to add to the donation count, the app crashes. Ive followed all the teachings of my teacher. I create a method in the activity, the in the ui builder I go to the button on click property and set it to that method. It should go smoothly but it crashes whenever I click the button. This is my activity which I set the onClick property to 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);
}

This is the model:

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

}

Ive even tried without the number format just to see if it sends back to the activity but it doesn't. BTW for my class I'm using an older version of android studio.

This is the logcat:

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) a t 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)

It seems like your textAdded is an empty String, which causes the NumberFormatException and crashes your app. What you can do to prevent this, is to check if your textAdded is not empty before parsing it:

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

Or just prevent the method from being invoked before the user has entered a value in the 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);
    }
}

I don't see where double addedDonation; is initialized, you can't use += when the variable has no value.

The problem is that you attempt to parse an empty string as a double. You need to verify that the user entered valid data before doing any calculations. One way to help you do this is to separate the calculation from the String passing and formatting. I suggest that you modify calcDonation() so that it only handles numeric data. Another method can worry about converting numbers to and from Strings. This will allow you to validate user input and display an appropriate error message when necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM