java.lang.NumberFormatException: Invalid int: "" : 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34947203/
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
java.lang.NumberFormatException: Invalid int: "" : Error
提问by Farrukh Faizy
I am doing some calculation but unable to parse a string into int or even in float.I searched for solution and i read it somewhere there must be a empty string but i checked my editText using
我正在做一些计算,但无法将字符串解析为 int 或什至浮点数。我搜索了解决方案,我在某处读取它必须有一个空字符串,但我使用
log.v("Valuee",e1.getText().toString());
and its print the values prove that string is not empty.. What i am missing ?
它打印的值证明字符串不为空..我错过了什么?
Here is logcat
这是logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.farrukh.bmi/com.example.farrukh.bmi.Main2Activity}: java.lang.NumberFormatException: Invalid int: "" at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NumberFormatException: Invalid int: "" at java.lang.Integer.invalidInt(Integer.java:137) at java.lang.Integer.parseInt(Integer.java:358) at java.lang.Integer.parseInt(Integer.java:331) at com.example.farrukh.bmi.Main2Activity.onCreate(Main2Activity.java:31) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)? at android.app.ActivityThread.access$800(ActivityThread.java:135)? at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)? at android.os.Handler.dispatchMessage(Handler.java:102)? at android.os.Looper.loop(Looper.java:136)? at android.app.ActivityThread.main(ActivityThread.java:5017)? at java.lang.reflect.Method.invokeNative(Native Method)?
run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) 引起:java.lang.NumberFormatException: Invalid int : "" at java.lang.Integer.invalidInt(Integer.java:137) at java.lang.Integer.parseInt(Integer.java:358) at java.lang.Integer.parseInt(Integer.java:331) at com .example.farrukh.bmi.Main2Activity.onCreate(Main2Activity.java:31) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android。 app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)?在 android.app.ActivityThread.access$800(ActivityThread.java:135)?在安卓。app.ActivityThread$H.handleMessage(ActivityThread.java:1196)?在 android.os.Handler.dispatchMessage(Handler.java:102)?在 android.os.Looper.loop(Looper.java:136)?在 android.app.ActivityThread.main(ActivityThread.java:5017)?在 java.lang.reflect.Method.invokeNative(Native Method)?
Here is MainActivity.java
这是MainActivity.java
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final Button b = (Button) findViewById(R.id.button);
final EditText e1 = (EditText) findViewById(R.id.editText2);
final TextView text = (TextView) findViewById(R.id.textView4);
final String height = e1.getText().toString();
final int a = Integer.parseInt(height); //got error while parsing
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Log.v("EditText",e1.getText().toString());
}
});
}
}
Here is activity.xml
这是activity.xml
<Button
android:layout_width="132dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center_horizontal"
android:text="CLICK"
android:clickable="true"
android:id="@+id/button"/>
<EditText
android:layout_width="120dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText2"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView4"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:layout_marginTop="20dp"
android:textColor="#3b9ff0" />
采纳答案by Ravi
put these lines inside onClick()
把这些线里面 onClick()
final String height = e1.getText().toString();
final int a = Integer.parseInt(height);
You are fetching value of e1
in onCreate()
, while you want it when user click on button
您正在获取e1
in 的值onCreate()
,而您在用户单击按钮时需要它
Also need to check whether height is having any value or not, check Stuluske'sanswer for this
还需要检查高度是否有任何价值,请查看Stuluske 对此的回答
回答by laalto
There's no value yet when onCreate()
runs. Move the getText()
and parseInt()
inside your click listener to read and parse the value when you have entered something.
onCreate()
运行时还没有价值。当您输入某些内容时,将getText()
和移动到parseInt()
您的点击侦听器中以读取和解析该值。
回答by Stultuske
You are trying to parse an empty String ( "" ) to a numerical value, but "" is not a numerical value.
您正在尝试将空字符串 ( "" ) 解析为数值,但 "" 不是数值。
Make sure you set it to required, or check for emptiness before trying to parse it.
确保将其设置为 required,或者在尝试解析之前检查是否为空。
final int a = !height.equals("")?Integer.parseInt(height) : 0;
for instance.
例如。
EDIT:
编辑:
If you have added spaces, so it would be " ";
如果你添加了空格,那就是“”;
height = height.trim();
final int a = !height.equals("")?Integer.parseInt(height) : 0;
should do the trick.
应该做的伎俩。
回答by Rajen Raiyarela
put a check before doing Integer.parseInt like
在做 Integer.parseInt 之前检查一下
final int a = (height == null || height.trim().equal("") ? 0 : Integer.parseInt(height));
Also you need to add this code for getting the height value from edittext inside onclick listener as when in OnCreate control will not have any value apart you set some default value in layout.xml as it is just created
您还需要添加此代码以从 onclick 侦听器内的 edittext 获取高度值,因为在 OnCreate 控件中将没有任何值,您在 layout.xml 中设置了一些默认值,因为它刚刚创建
回答by Austine Gwa
you should get the values inside a method so that your app does not try to assign the values immediatly you are creating them check below how am doing it using kotlin
您应该在方法中获取值,以便您的应用程序不会尝试立即分配您正在创建它们的值,请在下面检查我如何使用 kotlin
myresult = findViewById<TextView>(R.id.txtresult)
val1 = findViewById<EditText>(R.id.valone)
val2 = findViewById<EditText>(R.id.valtwo)
after getting the field use method as below
得到如下现场使用方法后
fun calculate() : Int {
var value1 = Integer.parseInt(val1.text.toString())
var value2 = val2.text.toString().toInt()
var result : Int
when (opType){
"+" ->{result = value1 + value2
return result
}
"-" ->{result = value1 - value2
return result
}
"*" -> {result = value1 * value2
return result
}
"/" -> {result = value1 / value2
return result
} else -> result = 20
}
return result
}
btn.setOnClickListener{
println(calculate().toString())
myresult.text = calculate().toString()
}
for those moving to kotlin and finding the same error I hope this code will help you
对于那些转向 kotlin 并发现相同错误的人,我希望这段代码对您有所帮助