java 从edittext获取整数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12882490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get integer value from edittext
提问by theduman
When I try to run the code below, my program has stops. I want this code to get the value from edittext
but it′s not working as I expected it. What am I doing wrong?
当我尝试运行下面的代码时,我的程序停止了。我希望这段代码从中获取值,edittext
但它没有按我预期的那样工作。我究竟做错了什么?
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sum = (Button) findViewById(R.id.button1);
Button cancel = (Button) findViewById(R.id.button2);
final EditText from = (EditText) findViewById(R.id.editText1);
final EditText upto = (EditText) findViewById(R.id.editText2);
final EditText runsum = (EditText) findViewById(R.id.editText3);
final int f = Integer.parseInt(from.getText().toString());
final int u = Integer.parseInt(upto.getText().toString());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Here is my LogCat sorry for mess :/
这是我的 LogCat 抱歉弄乱了:/
08-28 19:34:44.241: E/Trace(3346): error opening trace file: No such file or directory (2)
08-28 19:34:44.780: D/AndroidRuntime(3346): Shutting down VM
08-28 19:34:44.780: W/dalvikvm(3346): threadid=1: thread exiting with uncaught exception(group=0x40a13300)
08-28 19:34:44.790: E/AndroidRuntime(3346): FATAL EXCEPTION: main
08-28 19:34:44.790: E/AndroidRuntime(3346): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
08-28 19:34:44.790: E/AndroidRuntime(3346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-28 19:34:44.790: E/AndroidRuntime(3346): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-28 19:34:44.790: E/AndroidRuntime(3346): at android.app.ActivityThread.access0(ActivityThread.java:130)
08-28 19:34:44.790: E/AndroidRuntime(3346): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-28 19:34:44.790: E/AndroidRuntime(3346): at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 19:34:44.790: E/AndroidRuntime(3346): at android.os.Looper.loop(Looper.java:137)
08-28 19:34:44.790: E/AndroidRuntime(3346): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-28 19:34:44.790: E/AndroidRuntime(3346): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 19:34:44.790: E/AndroidRuntime(3346): at java.lang.reflect.Method.invoke(Method.java:511)
08-28 19:34:44.790: E/AndroidRuntime(3346): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-28 19:34:44.790: E/AndroidRuntime(3346): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-28 19:34:44.790: E/AndroidRuntime(3346): at dalvik.system.NativeStart.main(Native Method)
08-28 19:34:44.790: E/AndroidRuntime(3346): Caused by: java.lang.NumberFormatException: Invalid int: ""
08-28 19:34:44.790: E/AndroidRuntime(3346): at java.lang.Integer.invalidInt(Integer.java:138)
08-28 19:34:44.790: E/AndroidRuntime(3346): at java.lang.Integer.parseInt(Integer.java:359)
08-28 19:34:44.790: E/AndroidRuntime(3346): at java.lang.Integer.parseInt(Integer.java:332)
08-28 19:34:44.790: E/AndroidRuntime(3346): at com.example.project.MainActivity.onCreate(MainActivity.java:25)
08-28 19:34:44.790: E/AndroidRuntime(3346): at android.app.Activity.performCreate(Activity.java:5008)
08-28 19:34:44.790: E/AndroidRuntime(3346): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-28 19:34:44.790: E/AndroidRuntime(3346): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-28 19:34:44.790: E/AndroidRuntime(3346): ... 11 more
08-28 19:35:10.560: E/Trace(3394): error opening trace file: No such file or directory (2)
08-28 19:35:11.629: D/gralloc_goldfish(3394): Emulator without GPU emulation detected.
08-28 19:48:19.295: I/Choreographer(3394): Skipped 41 frames! The application may be doing too much work on its main thread.
回答by Bartosz Przybylski
The problem seems that when Activity is bringed to front there is no value in EditText
. Integer parser don't know how to parse empty String, so Exception is thrown.
问题似乎是,当 Activity 出现在前面时,EditText
. 整数解析器不知道如何解析空字符串,所以抛出异常。
You need to check for existing of text in that EditText
before parsing
EditText
在解析之前,您需要检查其中是否存在文本
final int f = -1; // or other invalid value
if (from.getText().toString().length() > 0)
f = Integer.parseInt(from.getText().toString());
Similarly for other EditText
同样对于其他 EditText
回答by thepoosh
as you can see from your Logcat:
正如您从 Logcat 中看到的:
08-28 19:34:44.790: E/AndroidRuntime(3346): Caused by: java.lang.NumberFormatException: Invalid int: ""
08-28 19:34:44.790: E/AndroidRuntime(3346): 引起: java.lang.NumberFormatException: Invalid int: ""
there was an empty input that couldn't be converted into an int
.
有一个空输入无法转换为int
.
try validating user input.
尝试验证用户输入。
回答by Kumar Vivek Mitra
-Please check the value of EditText
, is it a validinteger
value or not.
-请检查的值EditText
,它是一个有效的integer
值或没有。
Try out this code:
试试这个代码:
int i = 0;
try{
i = Integer.parseInt(from.getText().toString());
}catch(NumberFormatException ex){
System.out.println("Value at TextView is not a valid integer");
}