2013-02-17 67 views
0

所以基本上,我剛開始android開發,並從YouTube上的教程建立一個應用程序。我決定離開了一段時間,按照我想要的方式完成,現在我收到了一個錯誤。Edittext int崩潰

我想要做的是允許EditText決定這個計數器應用程序的增量。這裏是我的代碼:

Java的活動:

 counter = 0; 
     reset = (Button) findViewById(R.id.reset); 
     addone = (Button) findViewById(R.id.add); 
     takeOne = (Button) findViewById(R.id.take); 
     tvCounter = (TextView) findViewById(R.id.fullscreen_content); 
     incrementer = (EditText) findViewById(R.id.number); 

     final int increment = Integer.parseInt(incrementer.getText().toString()); 

     addone.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       counter += increment; 
       tvCounter.setText("" + counter); 
      } 
     }); 

     takeOne.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       counter -= increment; 
       tvCounter.setText("" + counter); 
      } 
     }); 

     reset.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       counter = 0; 
       tvCounter.setText("" + counter); 
      } 

XML:

 <Button 
      android:id="@+id/add" 
      style="?buttonBarButtonStyle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Add One" /> 
     <Button 
      android:id="@+id/take" 
      style="?buttonBarButtonStyle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Take One" /> 

     <Button 
      android:id="@+id/reset" 
      style="?buttonBarButtonStyle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Reset" /> 
    </LinearLayout> 

    <EditText 
     android:id="@+id/number" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:ems="10" 
     android:inputType="number" > 

     <requestFocus /> 
    </EditText> 
+2

發佈堆棧跟蹤,並告訴我們如何包含上面的代碼(它在方法,類範圍等)。 – 2013-02-17 00:53:17

回答

0

它可能崩潰,因爲你的EditText的內容不是數字。 假設這是您的onCreate方法,您不需要在此處閱讀edittext。 在更新計數器之前,解析按鈕的onClick方法中edittext的內容。

而且,因爲你不知道什麼樣的文本的用戶輸入,你應該圍繞一個嘗試,趕上parseInt函數調用來阻止應用的情況下崩潰的EditText不包含數字。

android:text="1"添加到xml文件中的EditText中。

1
02-17 09:35:56.313: E/AndroidRuntime(3177): Caused by: java.lang.NumberFormatException: Invalid int: "" 
02-17 09:35:56.313: E/AndroidRuntime(3177):  at java.lang.Integer.invalidInt(Integer.java:138) 
02-17 09:35:56.313: E/AndroidRuntime(3177):  at java.lang.Integer.parseInt(Integer.java:359) 
02-17 09:35:56.313: E/AndroidRuntime(3177):  at java.lang.Integer.parseInt(Integer.java:332) 
02-17 09:35:56.313: E/AndroidRuntime(3177):  at 

這可能是錯誤和發現在這條線

final int increment = Integer.parseInt(incrementer.getText().toString()); 

這是更好地把默認值您EditText

<EditText 
     android:id="@+id/number" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:ems="10" 
     android:text="0" 
     android:inputType="number" > 
+0

好吧,我已經這樣做了,我知道這可能聽起來很愚蠢,但實際上並沒有增加數字。我是否必須添加一個按鈕或其實際註冊增量值的東西,而不是隻輸入它? – ReallyGoodPie 2013-02-17 11:03:49

+0

你不需要添加按鈕 – Geros 2013-02-17 13:43:16

0

你得空的EditText並試圖parseInt函數從「」,您將從Integer.parseInt(「」)方法得到NumberFormatException。

只要使用try/catch來捕捉和處理異常。

final int increment; 
try { 
    increment = Integer.parseInt(); 
} catch (NumberFormatException e) { 
    // Do what you want 
}