当前位置: 首页 > 知识库问答 >
问题:

JAVAlang.NumberFormatException:无效的int:::错误

颛孙品
2023-03-14

我正在做一些计算,但无法将字符串解析为int,甚至是float。我搜索了解决方案,在某个地方读到了它,肯定有一个空字符串,但我用

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

它的打印值证明字符串不是空的...我错过了什么?

这是logcat

JAVAlang.RuntimeException:无法启动活动组件信息{com.example.farrukh.bmi/com.example.farrukh.bmi.Main2Activity}:java。lang.NumberFormatException:android上的无效int:”。应用程序。活动线程。在android上执行LaunchActivity(ActivityThread.java:2195)。应用程序。活动线程。android上的handleLaunchActivity(ActivityThread.java:2245)。应用程序。活动线程。在Android上获得800美元(ActivityThread.java:135)。应用程序。android上的ActivityThread$H.handleMessage(ActivityThread.java:1196)。操作系统。汉德勒。android上的dispatchMessage(Handler.java:102)。操作系统。活套。android上的loop(Looper.java:136)。应用程序。活动线程。java上的main(ActivityThread.java:5017)。朗,反思一下。方法java上的Invokenactive(本机方法)。朗,反思一下。方法在com上调用(Method.java:515)。Android内部的操作系统。ZygoteInit$MethodandArgscaler。在com上运行(ZygoteInit.java:779)。Android内部的操作系统。合子体。dalvik的main(ZygoteInit.java:595)。系统原生艺术。主要(本机方法)由以下原因引起:java。lang.NumberFormatException:无效的int:“”在java上。整型。java上的invalidit(Integer.java:137)。整型。java上的parseInt(Integer.java:358)。整型。com上的parseInt(Integer.java:331)。实例法鲁克。体重指数。主要活动。android上的onCreate(Main2Activity.java:31)。应用程序。活动android上的performCreate(Activity.java:5231)。应用程序。仪器。android上的callActivityOnCreate(Instrumentation.java:1087)。应用程序。活动线程。在android上执行LaunchActivity(ActivityThread.java:2159)。应用程序。活动线程。handleLaunchActivity(ActivityThread.java:2245)

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

共有3个答案

岳劲
2023-03-14

当运行onCreate()时,还没有任何值。将getText()parseInt()移动到click listener中,在输入内容时读取并解析值。

巴照
2023-03-14

将这些行放入onClick()

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

您正在onCreate()中获取e1的值,而当用户单击按钮时,您需要它

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

苏运良
2023-03-14

您试图将空字符串 ( "" ) 解析为数值,但""不是数值。

确保将其设置为required,或者在尝试解析之前检查其是否为空。

final int a = !height.equals("")?Integer.parseInt(height) : 0;

例如。

编辑:

如果您添加了空格,那么它将是“”;

height = height.trim();
final int a = !height.equals("")?Integer.parseInt(height) : 0;

我们应该做到这一点。

 类似资料:
  • 问题内容: 我正在做一些计算,但无法将字符串解析为int甚至是float。我寻找解决方案,并且在某个必须有空字符串的地方读取了它,但是我使用 及其打印的值证明该字符串不为空。 这是 logcat java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.farrukh.bmi / com.example.farrukh.bmi.Main

  • 我正在进行一些计算,但无法将字符串解析为int或float。我搜索了解决方案,并在某个地方读到了它,其中肯定有一个空字符串,但我检查了我的editText,使用 和它的打印值证明字符串不是空的..我错过了什么? java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.farrukh.bmi/com.example.farr

  • 我想创建一个简单的3x3矩阵类,并能够通过下标操作符访问其内容。代码如下: 我希望能够访问数组的元素,无论矩阵的对象是常量还是非常量。但我从编译器中得到以下错误: 错误:从“const int*”到“int*”的转换无效[-fpermissive] 我做了一些研究,我有一个假设:也许,因为我已经将这个成员函数声明为const函数,在它的定义中,编译器将对象的所有不可变成员视为const成员(它掩盖

  • 之前,我已经实施了此功能,并且它起到了作用: 我甚至可以用下面的东西检查我是否得到了正确的尺寸,一切都很好。 但是,在使用以下初始化尝试整个程序时,我得到了一个编译错误: 以下是错误消息: rbm. cpp:在函数“ululemexFunction(int, mxArray**, int, const mxArray**)”中:rbm. cpp: 570:64: error:从“int”到“int

  • 我试图读取CSV文件,但出现以下错误 谁能告诉我上面的代码有什么问题吗?我正在使用Windows10。 在我使用之后,我仍然有

  • 我被代码卡住了,无法继续。模拟器崩溃,堆栈指向第47行。请帮助初学者! 堆栈错误: 致命的例外:主java。lang.NumberFormatException:无效的int:“t”在java中。整型。invalidInt(Integer.java:138)