应用程序因NumberFormatException

时间:2018-07-19 15:26:29

标签: android runtimeexception numberformatexception

代码

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    EditText a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        a = (EditText) findViewById(R.id.a);

        int i = Integer.parseInt(a.getText().toString());
  }
}

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <EditText
        android:id="@+id/a"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

例外

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.shoaib.demo / com.example.shoaib.demo.MainActivity}:   java.lang.NumberFormatException:对于输入字符串:“”                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2892)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)                         在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)                         在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101)                         在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1786)                         在android.os.Handler.dispatchMessage(Handler.java:106)                         在android.os.Looper.loop(Looper.java:164)                         在android.app.ActivityThread.main(ActivityThread.java:6656)                         在java.lang.reflect.Method.invoke(本机方法)                         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)                      原因:java.lang.NumberFormatException:对于输入字符串:“”                         在java.lang.Integer.parseInt(Integer.java:629)                         在java.lang.Integer.parseInt(Integer.java:652)                         在com.example.shoaib.demo.MainActivity.onCreate(MainActivity.java:17)                         在android.app.Activity.performCreate(Activity.java:7117)                         在android.app.Activity.performCreate(Activity.java:7108)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1262)

2 个答案:

答案 0 :(得分:1)

出现此错误的原因是因为尚未在UI上创建EditText

但是,在调用Integer.parseInt之前,您需要屏幕上的 EditText以及一些有效数据

您需要使用某种界面,通过该界面您可以调用Integer.parseInt。我建议使用按钮。

如果您不想添加按钮,请使用以下代码:

a.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        int i = Integer.parseInt(a.getText().toString());
        //any operation involving i
        return true;
    }
});

然后,在a中键入一些数据后,长按a,然后看一下魔术!

答案 1 :(得分:0)

问题是您正在尝试将非int字符(“”--由于edittext可能为空)转换为int字符。 您可以在下面尝试此代码;

String input = a.getText().toString();

if (TextUtils.isDigitsOnly(input)) {
     i = Integer.parseInt(input);
     // do everything you want with your int here
}