小编典典

java.lang.NumberFormatException:无效的int:“”:错误

java

我正在做一些计算,但无法将字符串解析为int甚至是float。我寻找解决方案,并且在某个必须有空字符串的地方读取了它,但是我使用

log.v("Valuee",e1.getText().toString());

及其打印的值证明该字符串不为空。

这是 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)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),位于android.os.Handler.dispatchMessage(Handler.java:102),位于android.os.Looper.loop(Looper.java:136),位于android.app.ActivityThread.main(ActivityThread.java:5017)在com.android.internal.os.ZygoteInit
$
MethodAndArgsCaller处的java.lang.reflect.Method.invoke(Method.java:515)处的java.lang.reflect.Method.invokeNative(本机方法)。com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)在dalvik.system.NativeStart.main(Native
Method)处运行(ZygoteInit.java:779)原因:java.lang.NumberFormatException:Int int
:com的java.lang.Integer.parseInt(Integer.java:358)处的java.lang.Integer.invalidInt(Integer.java:137)处的“”
.example.farrukh.bmi.Main2Activity.onCreate(Main2Activity.java:31)在android.app.Activity.performCreate(Activity.java:5231)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)在android。
app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)在android.app.ActivityThread.access
$
800(ActivityThread.java:135)在android。android.os.Handler.dispatchMessage(Handler.java:102)上的app.ActivityThread
$
H.handleMessage(ActivityThread.java:1196)android.os.Looper.loop(Looper.java:136)上的android.app.ActivityThread
.main(ActivityThread.java:5017),位于java.lang.reflect.Method.invokeNative(本机方法)

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

                }
            });


        }


    }

这是 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" />

阅读 313

收藏
2020-11-26

共1个答案

小编典典

把这些线放进去 onClick()

final  String height = e1.getText().toString();
final int a = Integer.parseInt(height);

当用户单击按钮时,您想要e1in中的值onCreate()

2020-11-26