2012-05-09 46 views
3

我知道他已被問了幾次,但我一直在嘗試我找到的沒有運氣的一切。我仍然有錯誤。這是我的代碼。從EditText獲取號碼

XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:layout_gravity="center" 
android:background="@android:color/transparent" 
android:gravity="center" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Write item value." 
    android:textColor="@android:color/black" 
    android:textSize="25dp" /> 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="80dp" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="202dp" 
    android:ems="10" 
    android:hint="Value" 
    android:inputType="number" > 

    <requestFocus /> 
</EditText> 

的Java

public class PopupValores extends Activity { 

    EditText valor1; 
    String myEditValue; 
    public static int valor; 

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

     valor1 = (EditText) findViewById (R.id.editText1); 
     myEditValue = valor1.getText().toString(); 
     valor = Integer.parseInt(myEditValue); <<<<Line 20 

    } 
} 

logcat的

05-08 21:02:10.023: W/dalvikvm(6074): threadid=1: thread exiting with uncaught exception (group=0x40020578) 
05-08 21:02:10.039: E/AndroidRuntime(6074): FATAL EXCEPTION: main 
05-08 21:02:10.039: E/AndroidRuntime(6074): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dc.maker/com.dc.maker.PopupValores}: java.lang.NumberFormatException: unable to parse '' as integer 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at android.os.Looper.loop(Looper.java:130) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at java.lang.reflect.Method.invoke(Method.java:507) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at dalvik.system.NativeStart.main(Native Method) 
05-08 21:02:10.039: E/AndroidRuntime(6074): Caused by: java.lang.NumberFormatException: unable to parse '' as integer 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at java.lang.Integer.parseInt(Integer.java:362) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at java.lang.Integer.parseInt(Integer.java:332) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at com.popupclass.PopupValores.onCreate(PopupValores.java:20) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
05-08 21:02:10.039: E/AndroidRuntime(6074):  ... 11 more 

我試圖從EditText上得到一個int和在其它類使用確定一個值一些東西。有人能告訴我我做錯了什麼嗎?

感謝

+1

你得到一個錯誤,如果是的話請張貼logcat的痕跡。如果不是'valor'的價值是什麼? (哈,這很具有諷刺意味。) – Sam

+0

哈哈是的,它添加了LogCat。編輯:勇敢應該是編輯文本中用戶編輯的數字的值 –

+0

我gettint 0無論我在EditText上寫什麼,爲什麼? –

回答

3

唯一的例外是:

Java.lang.NumberFormatException: unable to parse '' as integer 

而且因爲在editbox1字段沒有值,這只是。

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

    valor1 = (EditText) findViewById (R.id.editText1); 
    myEditValue = valor1.getText().toString(); 

    Log.debug("logtag", myEditValue); // Here you can see the output. 

    try { 
     valor = Integer.parseInt(myEditValue); 
    } 
    catch(Exception e) { 
     Log.e("logtag", "Exception: " + e.toString()); 
    } 
} 
+0

無論我在EditText上寫什麼,我都會得到0,爲什麼? –

+0

此代碼僅在您創建活動時觸發。在運行時,EditText中沒有任何內容。 你只在編輯框中使用數字嗎? 另外,檢查myEditValue的字符串值,可能是'1'之類的東西。如果是這種情況,那麼Integer.parseInt將無法解析它。 – hallizh

1

您太快試圖訪問valor1valor1目前是一個空字符串。在用戶有機會定義某些內容之後,您必須處理該值。

嘗試添加一個按鈕這樣的:

(Button) button = (Button) findViewByID(R.id.button1); 
button.setOnClickListener(new OnClickListener() { 
    public void onClick(View view) { 
     String temp = valor1.getText().toString(); 
     if(temp.isEmpty() == false) { 
      valor = Integer.parseInt(temp); 
      Log.v("SO", "Valor = " + valor); 
     } 
    } 
} 
+0

好的,我會等待那段代碼,因爲那時我不知道該怎麼做。 –

+0

@Sam,我記得在Android SDK中不存在'String.isEmpty()'。 – neevek

+0

我很欣賞反饋,但這裏是[一些文檔](http://developer.android.com/reference/java/lang/String.html#isEmpty%28%29) – Sam

0

你上面的代碼肯定會導致問題,因爲你在onCreate()Integer.parseInt(myEditValue),在正在創建您的活動的時候,你的EditText尚未填充任何文本(並且您沒有在其XML定義中提供默認值),所以它是一個空字符串,並且Integer.parseInt(emptyString)將拋出NumberFormatException

正確的做法是將解析EditText值的代碼移動到某處,以響應用戶事件,或者簡單地將其移動到某個地方,或者簡單地將代碼解析爲EditText的值。

最安全的方式是始終try...catchInteger.parseInt(),因爲我們應該永遠不要相信用戶的輸入

1

代碼試圖從EditText''中解析出一個空字符串作爲int,這會導致異常被拋出。

您的示例代碼也丟失了關閉的LinearLayout標記。

1

使用正則表達式...下面:

public class PopupValores extends Activity { 

EditText valor1; 
String myEditValue; 
public static int valor; 

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

    valor1 = (EditText) findViewById (R.id.editText1); 
    myEditValue = valor1.getText().toString(); 
    valor = Integer.parseInt(myEditValue.replaceAll("[\\D]","")); 

} 

}