2013-02-17 103 views
1

當我嘗試將編輯文本值分配爲浮動時,我的應用程序強制關閉。 這是代碼。 我也試過Float.parseFloat()方法,而不是Float.valueOf()。 如果我脫掉我的getETvalue()方法,那麼該應用程序工作。請幫助嘗試將EditText值轉換爲浮點變量時強制關閉

package com.example.aviator_18; 

import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Color; 
import android.view.Menu; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    float Remaining,Departure,TotUplift,SG,DiscResult; 
    int CalUpliftResult; 
    TextView RemainingTV,DepartureTV,UpliftTV,SGtv,CalcUpliftTV,DiscrepancyTV; 
    EditText RemainingET,DepartureET,TotUpliftET,SGet,CalcUpliftET,DiscrepancyET; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     setupView(); 
     getETvalue(); 
     evaluation(); 
     CalcUpliftTV.setText(String.valueOf(CalUpliftResult)); 
     DiscrepancyTV.setText(String.valueOf(DiscResult)); 


    } 

    private void getETvalue() { 
     String a,b,c,d,e; 
     Remaining=Float.valueOf(RemainingET.getText().toString()); 
     Departure=Float.valueOf(DepartureET.getText().toString()); 
     TotUplift=Float.valueOf(TotUpliftET.getText().toString()); 
     SG=Float.valueOf(SGet.getText().toString()); 



    } 

    private void evaluation() { 


     CalUpliftResult=Math.round(TotUplift*SG); 
     DiscResult=((Departure-Remaining-CalUpliftResult)/CalUpliftResult)*100; 

    } 

    private void setupView() { 
     RemainingET=(EditText)findViewById(R.id.remainingET); 
     DepartureET=(EditText)findViewById(R.id.departureET); 
     TotUpliftET=(EditText)findViewById(R.id.TotalUpliftET); 
     SGet=(EditText)findViewById(R.id.SGet); 

     CalcUpliftTV=(TextView)findViewById(R.id.CalUpliftResult); 
     DiscrepancyTV=(TextView)findViewById(R.id.discResult); 



    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

} 
+0

我的整個應用程序崩潰之前,即使我給了一個輸入。所以我不認爲它的輸入是不可解析的 – 2013-02-17 13:21:42

+0

謝謝,當我包含異常處理應用程序atleast現在開始。 – 2013-02-17 13:30:08

+0

我解決了這個問題,由於解析不當,確實是個例外。我將getETvalue()函數放在按鈕onclick中,以便編輯文本值僅在輸入有效數據後才被解析爲浮動。 – 2013-02-17 13:49:29

回答

0

您需要確保該字符串可以解析爲float。 你可以添加try-catch

try { 
    Remaining=Float.parseFloat(RemainingET.getText().toString()); 
} catch (NumberFormatException nfe) { 
    // Handle exception 
} 
0

Float.valueOf將轉換包含字符串形式的浮點數的字符串值。但是,如果字符串中包含任何不能被解析爲一個浮點數,則此方法將拋出NumberFormatException

像:

float f = Float.valueOf("12.35") //this is correct

雖然

float f = Float.valueOf("abcd") //this is incorrect

導致力接近它必須投擲NumberFormatException

From the javadocs

的valueOf

public static Float valueOf(String s) 
        throws NumberFormatException 
Returns a Float object holding the float value represented by the argument string s. 

Throws: 
NumberFormatException - if the string does not contain a parsable number.