java.lang.NumberFormatException:无效的int:"":错误

Far*_*izy 2 java android parseint

我正在做一些计算,但无法将字符串解析为int甚至在float.I搜索解决方案,我在某处读取它必须有一个空字符串,但我检查我的editText使用

log.v("Valuee",e1.getText().toString());
Run Code Online (Sandbox Code Playgroud)

并且它打印的值证明字符串不是空的..我缺少什么?

这是logcat

java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.farrukh.bmi/com.example.farrukh.bmi.Main2Activity}:java.lang.NumberFormatException:无效的int:""在android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2195)在Android.app.ActivityThread.access $ 800(ActivityThread.java:135)的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)android.app.ActivityThread $ H.handleMessage(ActivityThread) .java:1196)android.app.Handler.dispatchMessage(Handler.java:102)android.app.Looper.loop(Looper.java:136)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)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)at dalvik.system.NativeStart.main(Native Method)引起:java.lang.NumberFormatException:无效的int:""at java.lang.Integer.invalidInt(Integer.java:137)在java.lang.Integer.parseInt(Integer.java:358)java.lang.Integer.parseInt(Integer.java:331)at com.example.farrukh .bmi.Main2Activity.onCreate(Main2Activity.java:31)位于android.app.A活动中的android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)上的android.app.Activity.performCreate(Activity.java:5231).在Android.app.ActivityThread.access $ 800(ActivityThread.java:135)的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)上的performLaunchActivity(ActivityThread.java:2159)在android.app.ActivityThread $ H.handleMessage( ActivityThread.java:1196)在Android.app.A.运行时,Android.A.Tharead.main上的android.os.Handler.dispatchMessage(Handler.java:102)android.os.Looper.loop(Looper.java:136)(ActivityThread.java:5017) )at java.lang.reflect.Method.invokeNative(Native Method) 

这是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());

                }
            });


        }


    }
Run Code Online (Sandbox Code Playgroud)

这是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" />
Run Code Online (Sandbox Code Playgroud)

Stu*_*ske 10

您正在尝试将空字符串("")解析为数值,但""不是数值.

确保将其设置为必需,或在尝试解析之前检查空白.

final int a = !height.equals("")?Integer.parseInt(height) : 0;
Run Code Online (Sandbox Code Playgroud)

例如.

编辑:

如果你有空格,那就是"";

height = height.trim();
final int a = !height.equals("")?Integer.parseInt(height) : 0;
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩.


Rav*_*avi 5

把这些线放进去 onClick()

final  String height = e1.getText().toString();
final int a = Integer.parseInt(height);
Run Code Online (Sandbox Code Playgroud)

当用户单击按钮时,您需要获取e1in的值onCreate()

还需要检查高度是否有任何值,请检查Stuluske的答案