得到了NumberFormatException

时间:2017-10-13 16:07:34

标签: java android

我是使用Android工作室的新手,我试图制作一个简单的平均分数计算器来解决问题。当我尝试将输入字符串解析为整数时,它会抛出此错误

Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:533)
at java.lang.Integer.parseInt(Integer.java:556)
at app.testscore.testscorecalculator.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)      
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

我编写了与普通java程序相同的代码,它工作正常。只是寻找一些帮助,说明为什么它不能在Android工作室工作,因为我是新手。

这是我的计算器代码:

package app.testscore.testscorecalculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import java.util.*;

public class MainActivity extends AppCompatActivity {

EditText txtScore1, txtScore2, txtScore3, txtScore4, txtScore5;
TextView lblAvgDisp;
int Score1, Score2, Score3, Score4, Score5, Average;
String First, Second, Third, Fourth, Fifth;

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

txtScore1 = (EditText) findViewById(R.id.txtScore1);
First = txtScore1.getText().toString();
Score1 = Integer.parseInt(First);
txtScore2 = (EditText) findViewById(R.id.txtScore2);
Second = txtScore2.getText().toString();
Score2 = Integer.parseInt(Second);
txtScore3 = (EditText) findViewById(R.id.txtScore3);
Third = txtScore3.getText().toString();
Score3 = Integer.parseInt(Third);
txtScore4 = (EditText) findViewById(R.id.txtScore4);
Fourth = txtScore4.getText().toString();
Score4 = Integer.parseInt(Fourth);
txtScore5 = (EditText) findViewById(R.id.txtScore5);
Fifth = txtScore5.getText().toString();
Score5 = Integer.parseInt(Fifth);

Average = (Score1 + Score2 + Score3 + Score4 + Score5) / 5;

Button btnCalcAvg = (Button) findViewById(R.id.btnCalcAvg);
btnCalcAvg.setOnClickListener(new View.OnClickListener()
    {
        public void onClick (View v)
        {
            lblAvgDisp.setText(Average);
        }
    });



}

4 个答案:

答案 0 :(得分:1)

您正在尝试将整数值设置为文本视图。你不能这样做

lblAvgDisp.setText(Average);

这样做

lblAvgDisp.setText(String.valueOf(Average));

答案 1 :(得分:0)

您的错误是

Caused by: java.lang.NumberFormatException: For input string: ""

因此我们必须判断String是否为空。

Score1 = Integer.parseInt(First);需要判断。

试试这个。

if(!TextUtils.isEmpty(First)){
    Score1 = Integer.parseInt(First);
}

if(!TextUtils.isEmpty(Second)){
    Score2 = Integer.parseInt(Second);
}
...

答案 2 :(得分:-1)

""不是一个数字因此是例外。您应该将解析包装在try catch中,或者在解析之前检查输入。

答案 3 :(得分:-1)

您收到该错误的原因是您尝试将空字符串解析为整数。

at app.testscore.testscorecalculator.MainActivity.onCreate(MainActivity.java:25)

First = txtScore1.getText().toString();

txtScore1不包含任何输入。