简体   繁体   English

构建后 Apk 崩溃

[英]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 APP 总结:2 个部分 第 1 部分有两个文本数字输入 + 1 个计算按钮 + 1 个 textview 结果 第 2 部分有两个文本数字输入 + 1 个计算按钮 + 1 个 textview 结果

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.现在,如果所有 4 个 textnumber 输入都不为空,如果您按下按钮 1,它将计算结果并将其放入结果 1,但会将结果 2 重置为 0。如果我按下第二个按钮,则会发生相反的情况。 Calculation is done correctly, correclty placed in result 2, but result 1 is reset to 0.计算正确完成,正确放置在结果 2 中,但结果 1 重置为 0。

But, if any of the 4 inputs happens to be empty, the APP will just quit.但是,如果 4 个输入中的任何一个为空,则 APP 将退出。 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:inputType输入数字,如下所示:

<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). Float.parseFloatonClick方法中的错误是“java.lang.NumberFormatException:空字符串”(您的错误消息包含所有这些详细信息)。 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 .这意味着您的EditText之一有空文本(检查活动中的第 41 行: at com.example.salescalculator.MainActivity.onClick(MainActivity.java:44 )。因此您需要在解析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. String格式的值(除了可以转换为数字)不能转换为float或任何原始类型的数字,如Integerdoubleshort等。因此,首先检查输入值是否为空EditText并使EditText只输入来自 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 .每当您使用parse时,您必须用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.顺便说一句,即使在调试模式下也是一个问题,不仅在您发布 APK 时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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