冗余警告&仿真期间的应用崩溃(Android Studio)(Java)

时间:2016-12-02 04:19:37

标签: java android-studio

我在这个计算程序中有两个问题(我很清楚)(采用多个输入,运行计算,几个输出)。谢谢你的帮助!!

1)在Variable 'aResult' initializer 'Double.parseDouble(tvaResult.getText().toString()) is redundant cResult etResult beta1Result phiResult& MnResult

2)单击按钮后应用程序崩溃。这让我相信我的一些计算遇到了错误,或者按钮编码本身是不正确的。

phiMnResult

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

1 个答案:

答案 0 :(得分:0)

You are not using the values, aResult, cResult, etc until you calculate them inside the switch statement. So why initialize them using the text from the 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.

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