获取号码从的EditText [英] Get numbers from EditText

查看:155
本文介绍了获取号码从的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,他已经被问了几次,但我一直在努力的一切,我发现,没有运气。我仍然有一个错误。这是我的code。

I know that his have been asked a few times, but I have been trying everything I have found with no luck. I'm still having a error. Here's my code.

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中

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,然后用它在其他类确定性的东西的价值。谁能告诉我什么,我做错了什么?

I'm trying to get a int from the EditText and then use it in other class to determinate a value of something. Can someone tell me what I'm doing wrong?

感谢

推荐答案

唯一的例外是:

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

和它的唯一原因是在editbox1领域没有任何价值。

And it's only because there is no value in the editbox1 field.

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

这篇关于获取号码从的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆