0

我正在尝试验证 EditText 以进行强制检查。所以我用 hasText() 函数创建了 Validation.java。即使在使用这个之后,当在 EditText 中没有传递任何值而不是显示警告时,我的应用程序也会崩溃。在下面附上我的代码。

Homescreen.class

package com.test.tax;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.text.Editable;

import java.text.DecimalFormat;

import static com.test.tax.R.id.editText;
import static java.lang.Math.round;

public class HomeScreen extends AppCompatActivity {

    public  static String  Buy_Price = "a";
    public  static String  Sell_Price = "b";
    public  static String  QUANTITY = "c";
    public  static String  BROKERAGE = "d";
    public  static String  ActualProfitLoss_String = "e";
    public  static String  TurnOver_String = "f";
    public  static String  STT_String = "g";
    public  static String  ServiceTax_String = "h";
    public  static String  TotalCharge_String = "i";
    public  static String  x = "j";
    private static  String Strin ;

    EditText editText;
    EditText editText2;
    EditText editText3;
    EditText editText4;
    Button btnSubmit;

    boolean doubleBackToExitPressedOnce = false;

    @Override
    public void onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                doubleBackToExitPressedOnce=false;
            }
        }, 2000);
    }

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



    public void calculateProfit(View view) {
        Intent intent = new Intent(this, displayProfit.class);
        Bundle extras =new Bundle();
        editText=(EditText) findViewById(R.id.editText);
        editText.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                Validation.hasText(editText);
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){}
        });
        editText4=(EditText) findViewById(R.id.editText4);
        editText4.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                Validation.hasText(editText4);
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){}
        });
        editText2=(EditText) findViewById(R.id.editText2);
        editText2.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                Validation.hasText(editText2);
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){}
        });
        editText3=(EditText) findViewById(R.id.editText3);
        editText3.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                Validation.hasText(editText3);
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){}
        });

        btnSubmit = (Button) findViewById(R.id.button);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                Validation class will check the error and display the error on respective fields
                but it won't resist the form submission, so we need to check again before submit
                 */
            }
        });


        Log.d(Strin, "editText ::::");


        String buyPriceString = editText3.getText().toString();
        Float BuyPrice =  Float.parseFloat(buyPriceString);
        String quantityString = editText2.getText().toString();
        Float Quantity =  Float.parseFloat(quantityString);
        String sellPriceString = editText4.getText().toString();
        Float SellPrice =  Float.parseFloat(sellPriceString);
        String brokerageString = editText.getText().toString();
        Float Brokerage =  Float.parseFloat(brokerageString);
        Log.d(Strin, "SellPrice ::::"+SellPrice);
        Log.d(Strin, "BuyPrice ::::"+BuyPrice);
        Log.d(Strin, "Quantity ::::"+Quantity);
        Log.d(Strin, "Brokerage ::::"+Brokerage);
        double TurnOver=(BuyPrice*Quantity)+(SellPrice*Quantity);
        double TurnOver1=roundTwoDecimals(TurnOver);
        Log.d(Strin, "TurnOver ::::"+TurnOver);
        double Sell1 =SellPrice-(SellPrice*(Brokerage/100));
        Log.d(Strin, "Sell1 ::::"+Sell1);
        double Buy1 = BuyPrice+(BuyPrice*(Brokerage/100));
        Log.d(Strin, "Buy1 ::::"+Buy1);
        double TotalBrokerage = ((Sell1*Brokerage/100)+(Buy1*Brokerage/100));
        Log.d(Strin, "TotalBrokerage ::::"+TotalBrokerage);


        double STT = (Quantity*(SellPrice-(SellPrice*(Brokerage/100))))*(.025/100);
        double STT1 = roundTwoDecimals(STT);

        Log.d(Strin, "STT ::::"+STT);
        double TrnxChrge = TurnOver*(0.00275/100);

        Log.d(Strin, "TrnxChrge ::::"+TrnxChrge);
        double ServiceTax = (TotalBrokerage+STT)*(.15);
        double ServiceTax1 = roundTwoDecimals(ServiceTax);

        Log.d(Strin, "ServiceTax ::::"+ServiceTax);
        double SEBICharge = (TurnOver*(.0002/100));
        Log.d(Strin, "SEBICharge ::::"+SEBICharge);
        double TotalCharge = ServiceTax+TrnxChrge+SEBICharge+STT;
        double TotalCharge1 = roundTwoDecimals(TotalCharge);
        Log.d(Strin, "TotalCharge ::::"+TotalCharge);
        double NetProfit = (Sell1-Buy1)*Quantity;
        Log.d(Strin, "NetProfit ::::"+NetProfit);

        double ActualProfitLoss = (NetProfit -TotalCharge);
        double ActualProfitLoss1 = roundTwoDecimals(ActualProfitLoss);
        String ActualProfitLossString=Double.toString(ActualProfitLoss1);
        String TurnOverString=Double.toString(TurnOver1);
        String STTString=Double.toString(STT1);
        String ServiceTaxString=Double.toString(ServiceTax1);
        String TotalChargeString=Double.toString(TotalCharge1);

        Log.d(Strin, "Pavi ::::"+ActualProfitLossString);

        extras.putString(ActualProfitLoss_String,ActualProfitLossString);
        extras.putString(TurnOver_String,TurnOverString);
        extras.putString(STT_String,STTString);
        extras.putString(ServiceTax_String,ServiceTaxString);
        extras.putString(TotalCharge_String,TotalChargeString);
        extras.putString(Buy_Price,buyPriceString);
        extras.putString(Sell_Price,sellPriceString);
        extras.putString(BROKERAGE,brokerageString);

        extras.putString(QUANTITY,quantityString);

        Log.d(Strin, "buyPrice ::::"+ActualProfitLoss);
//        intent.putExtra(Sell_Price,sellPriceString);
//        intent.putExtra(QUANTITY,quantity);
        intent.putExtras(extras);

        startActivity(intent);

    }

    private double roundTwoDecimals(double d) {
        DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
    }
}

验证.java

public class Validation {
public static boolean hasText(EditText editText) {
    String text = editText.getText().toString().trim();
    editText.setError(null);
    // length 0 means there is no text
    if (text.length() == 0) {
        editText.setError("REQUIRED_Data");
        return false;
    }
    return true;
}
}

调试日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.test.tax, PID: 3290
              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:5610)
                  at android.view.View$PerformClick.run(View.java:22265)
                  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:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:5610) 
                  at android.view.View$PerformClick.run(View.java:22265) 
                  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:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
               Caused by: java.lang.NumberFormatException: empty String
                  at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
                  at java.lang.Float.parseFloat(Float.java:459)
                  at com.test.tax.HomeScreen.calculateProfit(HomeScreen.java:121)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:5610) 
                  at android.view.View$PerformClick.run(View.java:22265) 
                  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:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'
4

2 回答 2

0

您的问题显示在

          Caused by: java.lang.NumberFormatException: empty String
              at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071)
              at java.lang.Float.parseFloat(Float.java:459)
              at com.test.tax.HomeScreen.calculateProfit(HomeScreen.java:121)
              at java.lang.reflect.Method.invoke(Native Method) 

你的parseFloat方法之一calculateProfit就是扔这个。这是由于有一个空字符串。

在解析为浮点数之前,将所有空字符串转换为零,例如:

   buyPriceString = buyPriceString.length() > 0 ? buyPriceString : "0";
于 2017-02-22T18:29:46.573 回答
0

此行有错误(任何类似的行都会抛出相同的错误)

    Float BuyPrice =  Float.parseFloat(buyPriceString);

EditText使用Validation.hasText().

例如,像这样,这些行:

String buyPriceString = editText3.getText().toString();
Float BuyPrice =  Float.parseFloat(buyPriceString);

应该是这样

   if(Validation.hasText(editText3)){
            String buyPriceString = editText3.getText().toString();
            Float BuyPrice =  Float.parseFloat(buyPriceString);
        }

记住要和你所有的其他人一起做edittexts

此外,您可能希望将Validation.hasText()工作方式更改为这种方式:

 if (text.equals("") ) {
        editText.setError("REQUIRED_Data");
        return false;
    }
于 2017-02-22T18:36:03.803 回答