【问题标题】:Get numbers from EditText从 EditText 获取数字
【发布时间】:2012-05-17 12:31:46
【问题描述】:

我知道他已经被问过几次了,但我一直在尝试我找到的所有东西,但没有运气。我仍然有一个错误。这是我的代码。

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

谢谢

【问题讨论】:

  • 您是否遇到错误,如果是,请发布 logcat 跟踪。如果不是,valor 的值是多少? (哈,这很讽刺。)
  • 哈哈,是的,LogCat 补充说。编辑: valor 应该是用户在 EditText 中写入的数字的值
  • 无论我在 EditText 上写什么,我都会得到 0,这是为什么呢?

标签: java android string android-edittext int


【解决方案1】:

例外是:

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

【讨论】:

  • 无论我在 EditText 上写什么,我都会得到 0,这是为什么呢?
  • 此代码仅在您创建活动时触发。在运行时,EditText 中没有任何内容。您是否仅在 EditBox 中使用数字?另外,检查 myEditValue 的字符串值,可能是 '1' 之类的东西。如果是这种情况,那么 Integer.parseInt 将无法解析它。
【解决方案2】:

代码试图将 EditText '' 中的空字符串解析为 int,这会导致引发异常。

您的示例代码也缺少结束 LinearLayout 标记。

【讨论】:

    【解决方案3】:

    您尝试访问valor1 太快,valor1 当前是一个空字符串。您必须在用户有机会定义某些内容后处理该值。

    尝试添加这样的按钮:

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

    【讨论】:

    • 好的,我会等待那个代码,因为那时我不知道该怎么做。
    • @Sam,我记得,String.isEmpty() 在 Android SDK 中不存在。
    • 感谢您的反馈,但这里是some documentation
    • 无论我在 EditText 上写什么,我都会得到 0,这是为什么呢?
    • 如果您使用已接受的答案,valor1 仍会被读取用户有机会输入任何文本之前。自己测试它,将android:text="562" 添加到EditText 的XML,现在接受的答案是562...您需要更改when 您阅读用户输入;就像他们按下“提交”按钮之后,valor1 失去焦点之后,因为您正在启动第二个活动,等等。
    【解决方案4】:

    使用正则表达式...如下:

    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]",""));
    
    }
    

    }

    【讨论】:

      【解决方案5】:

      您上面的代码肯定会引起问题,因为您在onCreate() 中做了Integer.parseInt(myEditValue),在创建Activity 时,您的EditText 尚未填充任何文本(并且您没有提供默认值在其 XML 定义中),所以它是一个空字符串,Integer.parseInt(emptyString) 将抛出 NumberFormatException

      执行此操作的正确方法是将解析 EditText 值的代码移动到某个地方,以响应用户事件,或者只是 try...catchInteger.parseInt()

      最安全的方法永远是try...catchInteger.parseInt(),因为我们应该永远不要相信用户的输入

      【讨论】:

        猜你喜欢
        • 2011-01-30
        • 2013-01-12
        • 2012-11-15
        • 2011-02-27
        • 2012-10-04
        • 1970-01-01
        • 2018-09-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多