2017-02-22 16 views
0

我試圖驗證EditText的強制性檢查。所以我用hasText()函數創建了Validation.java。即使在使用此應用程序後,如果在EditText中傳遞的值不是顯示警告,我的應用程序就會崩潰。 在下面附上我的代碼。如何驗證EditText。在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)); 
    } 
} 

Validation.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' 
+0

我真的不覺得錯誤是在'驗證'類! 你確定'(HomeScreen.java:121)'在'Validation'類中嗎? –

+0

是的,我雖然如此。我嘗試了共享的答案,它也不起作用。我認爲這個錯誤是由onClick()函數引起的。請分享解決這個問題。由於這個 – Pavithran

+0

'HomeScreen.java:121',我無法繼續進行下去,你的'HomeScreen.java'代碼中的121行是哪裏? –

回答

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) 

你在calculateProfit中的parseFloat方法之一就是扔這個。這是由於有一個空的字符串。

之前解析浮動,將所有空字符串爲零,例如:

buyPriceString = buyPriceString.length() > 0 ? buyPriceString : "0"; 
0

有錯誤是在這條線(及任何類似的線將拋出同樣的錯誤)

Float BuyPrice = Float.parseFloat(buyPriceString); 

你在Validation.hasText()的幫助下使用它之前,必須經常檢查是否有輸入值EditText

例如,像這樣,這些行:

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