简体   繁体   中英

Apk crashes after build

Editing previous post with better info:

APP summary: 2 sections Section 1 has two textnumber inputs + 1 calculation button + 1 textview result Section 2 has two textnumber inputs + 1 calculation button + 1 textview result

So, upon launch everything is fine, no crash.

Now, if all 4 textnumber inputs are not empty, if you press button 1 it will calculate result and put it in result 1, but will reset result 2 to 0. If I press on the second button, opposite happens. Calculation is done correctly, correclty placed in result 2, but result 1 is reset to 0.

But, if any of the 4 inputs happens to be empty, the APP will just quit. Current modified code is below and also debugging result at moment of crash.

package com.example.salescalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
        implements View.OnClickListener{

    Button Pricebutton, MaxCostbutton;
    EditText num1, num2, num3, num4;
    TextView result1, result2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        num1 = (EditText) findViewById(R.id.costinput);
        num2 = (EditText) findViewById(R.id.margininput);
        num3 = (EditText) findViewById(R.id.tpinput);
        num4 = (EditText) findViewById(R.id.margininput2);
        Pricebutton = (Button) findViewById(R.id.buttoncalcprice);
        MaxCostbutton = (Button) findViewById(R.id.buttoncalculatecost);
        result1 = (TextView) findViewById(R.id.priceresult);
        result2 = (TextView) findViewById(R.id.maxcostresult);

        Pricebutton.setOnClickListener(this);
        MaxCostbutton.setOnClickListener(this);
    }
            public void onClick(View v) {
        float cost1, margin1, custTP, margin2;
        float result1A = 0;
        float result2B = 0;

        cost1=Float.parseFloat(num1.getText().toString());
        margin1=Float.parseFloat(num2.getText().toString());
        custTP=Float.parseFloat(num3.getText().toString());
        margin2=Float.parseFloat(num4.getText().toString());

                if (!TextUtils.isEmpty(num1.getText().toString())) {
                    cost1 = Float.parseFloat(num1.getText().toString());
                }
                if (!TextUtils.isEmpty(num2.getText().toString())) {
                    margin1 = Float.parseFloat(num2.getText().toString());
                }
                if (!TextUtils.isEmpty(num3.getText().toString())) {
                    custTP = Float.parseFloat(num3.getText().toString());
                }
                if (!TextUtils.isEmpty(num4.getText().toString())) {
                    margin2 = Float.parseFloat(num4.getText().toString());
                }

        if(v.getId()==R.id.buttoncalcprice) {
            result1A = cost1 / (1- (margin1/100));
        }

        else if (v.getId()== R.id.buttoncalculatecost) {

            result2B = custTP - ((margin2/100)*custTP);
        }

                result1.setText(result1A + "");
                result2.setText(result2B + "");

            }
}

Crash Log

 --------- beginning of crash
2019-10-08 16:46:48.544 4994-4994/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.salescalculator, PID: 4994
    java.lang.NumberFormatException: empty String
        at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
        at java.lang.Float.parseFloat(Float.java:459)
        at com.example.salescalculator.MainActivity.onClick(MainActivity.java:44)
        at android.view.View.performClick(View.java:5637)
        at android.view.View$PerformClick.run(View.java:22429)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
2019-10-08 16:46:48.555 1381-1425/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 1473729 , only wrote 1473120  

At first it is really recommendable to learn about debugging, you'll save much time by doing that instead of running an app and then trying to figure out what the problem could be manually.

Secondly, I ran your code and it works. You don't make any checks wether the input is empty or not a number what you should do but if you give a correct input the code itself works.

Edit:

Your code with a change of your check might look like this for example:

 public void onClick(View v) {
    float cost1, margin1, custTP, margin2;
    float result1A = 0;
    float result2B = 0;


    if (!num1.getText().toString().isEmpty() && num1 != null){
        cost1 = Float.parseFloat(num1.getText().toString());
    } else {
        cost1 = 0f;
    }

    if (!num2.getText().toString().isEmpty() && num2 != null){
        margin1 = Float.parseFloat(num2.getText().toString());
    } else {
        margin1 = 0f;
    }

    if (!num3.getText().toString().isEmpty() && num3 != null){
        custTP = Float.parseFloat(num3.getText().toString());
    } else {
        custTP = 0f;
    }

    if (!num4.getText().toString().isEmpty() && num4 != null){
        margin2 = Float.parseFloat(num4.getText().toString());
    } else {
        margin2 = 0f;
    }


    if(v.getId()==R.id.buttoncalcprice) {
        result1A = cost1 / (1- (margin1/100));
    }

    else if (v.getId()== R.id.buttoncalculatecost) {

        result2B = custTP - ((margin2/100)*custTP);
    }

    result1.setText(result1A + "");
    result2.setText(result2B + "");
}

Also make sure that in the EditText you declare that you only want a number input through android:inputType like this:

<EditText
    android:id="@+id/costinput"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" />

The error is "java.lang.NumberFormatException: empty String" in your onClick method at Float.parseFloat (your error message contains all these details). It means that one of your EditText has empty text (check 41 line in your activity: at com.example.salescalculator.MainActivity.onClick(MainActivity.java:44 ). So you need to verify that text is not empty before parsing the Float .

Update: It seems you're trying to parse float before check:

cost1=Float.parseFloat(num1.getText().toString());
margin1=Float.parseFloat(num2.getText().toString());
custTP=Float.parseFloat(num3.getText().toString());
margin2=Float.parseFloat(num4.getText().toString());

if (!TextUtils.isEmpty(num1.getText().toString())) {
    cost1 = Float.parseFloat(num1.getText().toString());
}
if (!TextUtils.isEmpty(num2.getText().toString())) {
    margin1 = Float.parseFloat(num2.getText().toString());
}
if (!TextUtils.isEmpty(num3.getText().toString())) {
    custTP = Float.parseFloat(num3.getText().toString());
}
if (!TextUtils.isEmpty(num4.getText().toString())) {
    margin2 = Float.parseFloat(num4.getText().toString());
}

A String formatted value (except it can be converted into numbers) can't be converted into float or any primitive type of numbers, like Integer , double , short etc. So, first, check check if the input value is empty or not from the EditText and make the EditText takes input of only decimal numbers from xml.

Use this code which can solve your problem:

<EditText
     android:id="@+id/editText1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:ems="10"
     android:inputType="numberDecimal" />

Whenever you are using parse you must surround the area with try / catch . That's because empty strings cannot be parsed into numbers (floats, doubles, ints). Therfore:

try{
   if (!num1.getText().toString().isEmpty() && num1 != null){
        cost1 = Float.parseFloat(num1.getText().toString());
    } else {
        cost1 = 0f;
    }

    if (!num2.getText().toString().isEmpty() && num2 != null){
        margin1 = Float.parseFloat(num2.getText().toString());
    } else {
        margin1 = 0f;
    }

    if (!num3.getText().toString().isEmpty() && num3 != null){
        custTP = Float.parseFloat(num3.getText().toString());
    } else {
        custTP = 0f;
    }

    if (!num4.getText().toString().isEmpty() && num4 != null){
        margin2 = Float.parseFloat(num4.getText().toString());
    } else {
        margin2 = 0f;
    }

} catch(NumberFormatException e){
  e.printStackTrace()
}

And btw, that's a problem even in the debug mode, not only when you release APK.

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