2012-06-20 79 views
1

我試圖創建for -loop將視圖添加到佈局。 for -loop工作正常(我嘗試了初始化變量),但我需要從EditText得到一個整數。我用了一個try-catch和logcat的告訴我下面......從數值EditText字段獲取NumberFormatException

java.lang.NumberFormatException: invalid int "". 

網站developers.android.com說,這是從一個字符串錯誤地轉換爲整數,但我不明白我怎麼會不正確從EditText獲取數據。

這是我的代碼...

public class UserPref2Activity extends Activity 

{ 
@Override 

public void onCreate(Bundle savedInstanceState) 
{ 
    try 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     EditText numSensors = (EditText) findViewById(R.id.num_sensors); 
     String change = numSensors.getText().toString(); 
     int i = Integer.parseInt(change); 

     int j; //iterates through the for loop 

     ScrollView sv = new ScrollView(this); 
     LinearLayout ll = new LinearLayout(this); 
     ll.setOrientation(LinearLayout.VERTICAL); 
     sv.addView(ll); 

     for(j=1;j<i;j++) 
     { 
      EditText name = new EditText(this); 
      name.setText("Name:"); 
      EditText type = new EditText(this); 
      type.setText("Type:"); 
      EditText bits = new EditText(this); 
      bits.setText("Bits:"); 
      ll.addView(name); 
      ll.addView(type); 
      ll.addView(bits); 
     } 
     this.setContentView(sv); 
    } 
    catch (Exception e) 
    { 
     //sends actual error message to the log 
     Log.e("ERROR", "ERROR IN CODE:" + e.toString()); 
     //prints out location of error 
     e.printStackTrace(); 
    } 
    } 
} 

這是我的XML文件...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:id="@+id/userLayout" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Enter number of sensors:" /> 

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="number" 
    android:id="@+id/num_sensors" /> 

+0

是什麼使用我?什麼時候它永遠不會在屏幕上顯示? –

回答

0

我認爲初始編輯文本將是空的,所以 「」( blank is not a valid input)正在發生錯誤,您應該填寫編輯文本中的數據,然後點擊任何事件您應該進行解析工作....

1

如果您嘗試解析空字符串,它將始終拋出NumberFormatException

將您的代碼更改爲以下內容。

int i; 
String change = numSensors.getText().toString(); 
if(change.length>0) 
{ 
    i = Integer.parseInt(change); 
} 
else 
{ 
    i=0; 
} 
0

編輯您的佈局文件

<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:inputType="number" 
android:text="0" 
    android:id="@+id/num_sensors" /> 
0

,因爲沒有什麼或"" /空字符串最初在編輯文本您收到此錯誤。編輯文本仍然顯示時,獲取文本是沒有意義的。您可能希望有一個按鈕或其他事件,表示用戶完成了在編輯文本中輸入值的操作,然後您可以繼續並獲取編輯文本的值。但總是有對空字符串檢查

0

BEZ你逝去的空字符串中parseInt函數(),這樣解析字符串爲int前檢查爲:

EditText numSensors = (EditText) findViewById(R.id.num_sensors); 
String change = numSensors.getText().toString(); 
if (change.trim().equals("")) { 
//DO SOMETHING HERE 
} 
else 
{ 
int i = Integer.parseInt(change); 
YOUR CODE HERE.... 
} 
5

EditText最初有一個空String其價值(即"")。這顯然不是一個數字,所以當你嘗試撥打int i = Integer.parseInt(change);時,它會給你一個錯誤,就像這樣。

有幾種方法可以解決這個問題,但它基本歸結爲通過設置初始值或檢測空字符串並正確處理來防止錯誤。

爲了防止錯誤的發生......

EditText numSensors = (EditText) findViewById(R.id.num_sensors); 
String change = numSensors.getText().toString(); 
if (change.equals("")){ // detect an empty string and set it to "0" instead 
    change = "0"; 
} 
int i = Integer.parseInt(change); 

或爲EditText置初值爲"0",然而,這也顯示值0EditText在界面上,而不是空的,所以它可能不適合所有目的......

<EditText android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:inputType="number" 
      android:text="0" 
      android:id="@+id/num_sensors" /> 

如果要檢測錯誤,妥善處理,你可以這樣做......

EditText numSensors = (EditText) findViewById(R.id.num_sensors); 
String change = numSensors.getText().toString(); 
int i = 0; // set it to 0 as the default 
try { 
    i = Integer.parseInt(change); 
} 
catch (NumberFormatException e){} 

這基本上設置的i = 0價值,只會將其更改爲不同的值如果try{}代碼不會給出錯誤。

0

爲了防止一個int從得到一個「」值,即當有一個空的EditText場第二次點擊會發生什麼,你可以在你的Java代碼拋出此行:

int i; 
String change = numSensors.getText().toString(); 
if(!(change.equals(""))) 
{ 
    i = Integer.parseInt(change); 
}