简体   繁体   English

仿真期间出现冗余警告和应用崩溃(Android Studio)(Java)

[英]Redundant Warning & App Crash during emulation (Android Studio) (Java)

I have two issues (that I'm aware of) on this calculation program (taking several inputs, run calculation, several outputs). 我在此计算程序上有两个问题(我知道)(接受多个输入,运行计算,多个输出)。 Thanks for any help!! 谢谢你的帮助!!

1) Variable 'aResult' initializer 'Double.parseDouble(tvaResult.getText().toString()) is redundant similar warning on cResult etResult beta1Result phiResult MnResult & phiMnResult 1) Variable 'aResult' initializer 'Double.parseDouble(tvaResult.getText().toString()) is redundant cResult etResult beta1Result phiResult MnResultphiMnResult上的类似警告

2) Application is crashing after clicking the button. 2)单击该按钮后,应用程序崩溃。 This leads me to believe that either some of my calculations are running into errors, or the button coding itself is incorrect. 这使我相信,我的某些计算遇到了错误,或者按钮编码本身是不正确的。

--------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.ericallenbellville.rcbeamdesign, PID: 3087 java.lang.NumberFormatException: empty String at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071) at java.lang.Double.parseDouble(Double.java:547) at com.example.ericallenbellville.rcbeamdesign.MainActivity.onClick(MainActivity.java:54) 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)

    package com.example.ericallenbellville.rcbeamdesign;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public Button btnCalc;
    private TextView tvaResult;
    private TextView tvcResult;
    private TextView tvetResult;
    private TextView tvphiResult;
    private TextView tvMnResult;
    private TextView tvphiMnResult;
    private TextView tvbeta1Result;
    private EditText etB,etD,etAs,etFc,etFy;

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

    private void init() {
        btnCalc = (Button)findViewById(R.id.btnCalc);
        etB = (EditText)findViewById(R.id.etB);
        etD = (EditText)findViewById(R.id.etD);
        etAs = (EditText)findViewById(R.id.etAs);
        etFc = (EditText)findViewById(R.id.etFc);
        etFy = (EditText)findViewById(R.id.etFy);
        tvaResult = (TextView)findViewById(R.id.tvaResult);
        tvcResult = (TextView)findViewById(R.id.tvcResult);
        tvetResult = (TextView)findViewById(R.id.tvetResult);
        tvphiResult = (TextView)findViewById(R.id.tvphiResult);
        tvMnResult = (TextView)findViewById(R.id.tvMnResult);
        tvphiMnResult = (TextView)findViewById(R.id.tvphiMnResult);
        tvbeta1Result = (TextView)findViewById(R.id.tvbeta1Result);

        btnCalc.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
            Double B = Double.parseDouble(etB.getText().toString());
            Double D = Double.parseDouble(etD.getText().toString());
            Double As = Double.parseDouble(etAs.getText().toString());
            Double Fc = Double.parseDouble(etFc.getText().toString());
            Double Fy = Double.parseDouble(etFy.getText().toString());
            Double aResult = Double.parseDouble(tvaResult.getText().toString());
            Double cResult = Double.parseDouble(tvcResult.getText().toString());
            Double etResult = Double.parseDouble(tvetResult.getText().toString());
            Double beta1Result = Double.parseDouble(tvbeta1Result.getText().toString());
            Double phiResult = Double.parseDouble(tvphiResult.getText().toString());
            Double MnResult = Double.parseDouble(tvMnResult.getText().toString());
            Double phiMnResult = Double.parseDouble(tvphiMnResult.getText().toString());
            switch(view.getId()) {
                case R.id.btnCalc:
                    if (Fc <= 4000) {
                        beta1Result = (0.85);
                    } else if (4000 < Fc && Fc <= 8000) {
                        beta1Result = ((0.85)-(0.05 * ((Fc - 4000) / (1000))));
                    } else {
                        beta1Result = 0.65;
                    }
                    aResult = ((Fy * As) / (0.85 * Fc * B));
                    cResult = (aResult / beta1Result);
                    etResult = (((D - cResult) / (cResult)) * 0.003);
                    if (etResult >= 0.005) {
                        phiResult = (0.9);
                    } else if (0.002 <= etResult && etResult < 0.005) {
                        phiResult = (0.65 + (etResult - 0.002) * 0.25 / (0.005 - 0.002));
                    } else {
                        phiResult = (0.00);
                    }
                    MnResult = (((Fy * As) * (D - (aResult / 2.0))));
                    phiMnResult = phiResult * MnResult;
                    tvaResult.setText(String.valueOf(aResult));
                    tvcResult.setText(String.valueOf(cResult));
                    tvetResult.setText(String.valueOf(etResult));
                    tvphiResult.setText(String.valueOf(phiResult));
                    tvMnResult.setText(String.valueOf(MnResult));
                    tvphiMnResult.setText(String.valueOf(phiMnResult));
                    break;

            }}
    }

You are not using the values, aResult , cResult , etc until you calculate them inside the switch statement. 直到在switch语句中计算它们之前,才使用aResultcResult aResult So why initialize them using the text from the textviews? 那么,为什么要使用来自textviews的文本来初始化它们呢? The reason you are getting an exception, is because the textviews are empty and you are trying to parse empty string into a Double . 出现异常的原因是,由于textviews为空,并且您试图将空字符串解析为Double

double aResult = 0; //Double.parseDouble(tvaResult.getText().toString());
double cResult = 0; //Double.parseDouble(tvcResult.getText().toString());
double etResult = 0; //Double.parseDouble(tvetResult.getText().toString());
double beta1Result = 0; //Double.parseDouble(tvbeta1Result.getText().toString());
double phiResult = 0; //Double.parseDouble(tvphiResult.getText().toString());
double MnResult = 0; //Double.parseDouble(tvMnResult.getText().toString());
double phiMnResult = 0; //Double.parseDouble(tvphiMnResult.getText().toString());
switch(view.getId()) {
    case R.id.btnCalc:
    if (Fc <= 4000) {
        beta1Result = (0.85);
    } else if (4000 < Fc && Fc <= 8000) {
        beta1Result = ((0.85)-(0.05 * ((Fc - 4000) / (1000))));
    } else {
        beta1Result = 0.65;
    }
    aResult = ((Fy * As) / (0.85 * Fc * B));
    cResult = (aResult / beta1Result);
    etResult = (((D - cResult) / (cResult)) * 0.003);
    if (etResult >= 0.005) {
        phiResult = (0.9);
    } else if (0.002 <= etResult && etResult < 0.005) {
        phiResult = (0.65 + (etResult - 0.002) * 0.25 / (0.005 - 0.002));
    } else {
        phiResult = (0.00);
    }
    MnResult = (((Fy * As) * (D - (aResult / 2.0))));
    phiMnResult = phiResult * MnResult;
    tvaResult.setText(String.valueOf(aResult));
    tvcResult.setText(String.valueOf(cResult));
    tvetResult.setText(String.valueOf(etResult));
    tvphiResult.setText(String.valueOf(phiResult));
    tvMnResult.setText(String.valueOf(MnResult));
    tvphiMnResult.setText(String.valueOf(phiMnResult));
    break;
}

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

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