java - 从 EditText 获取数字

标签 java android string android-edittext int

我知道他已经被问过几次了,但我一直在尝试我发现的一切,但没有成功。我仍然有错误。这是我的代码。

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

    }
}

日志猫

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,然后在其他类中使用它来确定某物的值。谁能告诉我我做错了什么?

谢谢

最佳答案

异常(exception)情况是:

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

关于java - 从 EditText 获取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10508529/

相关文章:

java - Java中如何连接LinkedList?

java - 使用 instanceof 与将 isSomething() 添加到基类

android - adapter getCount 和 listView getChildCount 不相等

android - 具有多个 Activity 和单个启动器的应用程序

java - 处理 Rest API 中未找到的资源

java - Mockito - 感觉我没有充分发挥它的潜力

java - Galaxy S3 和 ACR122 之间的 P2P NFC 通信

mysql - 用子字符串替换列的值(未知长度)

c - malloc ** 和 * 之间有区别吗

c# - 向 String.Join 添加新行?