来自EditText问题的整数

时间:2014-11-20 11:15:29

标签: java android

我想添加2个数字并在屏幕上显示结果。 这就是为什么我有2个EditText(Number1& Number2),一个Button(PLUS)和一个显示结果的TextView。

问题是整数和EditText可能出现问题 Logcat说:"引起:java.lang.NumberFormatException:无效的int:"

这是代码:

public class MainActivity extends Activity {

EditText FirstNum;
EditText SecondNum;
Button PlusButton;
TextView Result;
int Number1;
int Number2;

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

    FirstNum = (EditText) findViewById(R.id.FirstNum);
    SecondNum = (EditText) findViewById(R.id.SecondNum);


        Number1 = Integer.parseInt(FirstNum.getText().toString());
        Number2 = Integer.parseInt(SecondNum.getText().toString());



    PlusButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Result.setText(plusmethod(Number1,Number2)+"");
        }
    });

}


int plusmethod(int a, int b) {
    return a + b;
}

更新:这是我的完整Logcat:

 Process: com.gss.dejix.calq, PID: 20008
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gss.dejix.calq/com.gss.dejix.calq.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2412)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
            at android.app.ActivityThread.access$900(ActivityThread.java:174)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            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:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.gss.dejix.calq.MainActivity.onCreate(MainActivity.java:36)
            at android.app.Activity.performCreate(Activity.java:5458)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2470)
            at android.app.ActivityThread.access$900(ActivityThread.java:174)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            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:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

6 个答案:

答案 0 :(得分:1)

移动代码

    Number1 = Integer.parseInt(FirstNum.getText().toString());
    Number2 = Integer.parseInt(SecondNum.getText().toString());

PlusButton.setOnClickListener(....)

  PlusButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

    if(!TextUtils.isEmpty(FirstNum.getText().toString()) && !TextUtils.isEmpty(SecondNum.getText().toString())){

     Number1 = Integer.parseInt(FirstNum.getText().toString());
     Number2 = Integer.parseInt(SecondNum.getText().toString());
     Result.setText(""+plusmethod(Number1,Number2)+"");

    }

    }
});

您还需要初始化TextView Result;

答案 1 :(得分:1)

在解析为int之前检查editext值,否则它会给出NumberFormatException:

if(FirstNum.getText().toString().trim().length()>0){
     Number1 = Integer.parseInt(FirstNum.getText().toString());
}
if(SecondNum.getText().toString().trim().length()>0){
     Number2 = Integer.parseInt(SecondNum.getText().toString());
}

您也缺少初始化Result和PlusButton,在使用前首先对其进行初始化。

示例:

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

        FirstNum = (EditText) findViewById(R.id.FirstNum);
        SecondNum = (EditText) findViewById(R.id.SecondNum);
        Result = (TextView) findViewById(R.id.Result);
        PlusButton = (Button) findViewById(R.id.PlusButton);

        PlusButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isValid=true;
                if(FirstNum.getText().toString().trim().length()<=0){
                    FirstNum.setError("Value Required");
                    isValid=false;
                }
                if(SecondNum.getText().toString().trim().length()<=0){
                    SecondNum.setError("Value Required");
                    isValid=false;
                }
                if(isValid){
                    Result.setText(String.valueOf(plusmethod(Integer.parseInt(FirstNum.getText().toString()),Integer.parseInt(SecondNum.getText().toString()))));
                }

            }
        });
    }

答案 2 :(得分:0)

您没有初始化textview结果

   Result =  (TextView ) findViewById(R.id.result);
   PlusButton=(Button)findViewById(R.id.plusbutton);

也这样做

PlusButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
     Number1 = Integer.parseInt(FirstNum.getText().toString());
     Number2 = Integer.parseInt(SecondNum.getText().toString());
    Result.setText(""+plusmethod(Number1,Number2)+"");
    }
});

答案 3 :(得分:0)

您必须在try ... catch块中编写代码,如下所示,

PlusButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
try{
    Result.setText(plusmethod(Number1,Number2)+"");
}catch(NumberFormatException e){
}
});

答案 4 :(得分:0)

很明显,您将使用Edittext中的仅获取数值。因此,您可以将编辑文本设为android:inputType =&#34; number&#34;

<EditText
android:id="@+id/edtPhoneNo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"/>

如果您正在使用:Number1 = Integer.parseInt(FirstNum.getText()。toString());也可以使用字母值。这就是你得到这个例外的原因。如果您没有获得解决方案,请将其添加到评论中。

答案 5 :(得分:0)

首先检查null或empty:之后用整数解析它:

public static boolean isNullOrEmpty(final String pStr) {
    return pStr == null || pStr.trim().length() == 0 || pStr.trim().equalsIgnoreCase("null");
}

&安培;编辑文本中的另一件事是进行验证

 android:digits="0123456789"