2017-10-13 3 views
-5

私はAndroidスタジオを初めて使用しています。私は物事のハングアップを得るために単純な平均スコア計算をしようとしていました。入力文字列を解析して整数に変換しようとしたときに、このエラーがスローされました。NumberFormatExceptionを取得しました

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プログラムと同じコードを書きましたが正常です。なぜそれがアンドロイドスタジオで働いていないのかについての助けを探して、私はそれに新しいです。 「」

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

空の文字列を解析しようとしています。解析する前の文字列を整数に出力し、何が問題になったのかを確認します。 –

+1

空の文字列が有効な番号ではないというエラーメッセージのどの部分があなたを混乱させていますか? ---表示されたコードは例外を引き起こしたコードではないことに注意してください。** Integer.parseInt()を呼び出すときに** line 25 **で例外が発生し、25行目にそのような呼び出しがありません示されたコードの26行目がありますが25行目はありませんので、コードは変更されています。 – Andreas

答えて

-1

番号ので、例外ではありません。ここで

は、電卓のための私のコードです。 tryキャッチで解析をラップするか、解析する前に入力をチェックする必要があります。

-1

エラーが発生するのは、空の文字列を解析して整数にするためです。

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

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

txtScore1入力がありません。

+0

それでは、入力をeditTextから文字列に変換する方法はありますか? –

+0

@MicahLudwickそれが空でないことを確認することによって。 –

0

あなた誤りだから我々はStringがnullであるかどうかを判断しなければならない

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

です。

そしてScore1 = Integer.parseInt(First);が判断する必要があります。

これを試してください。

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

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

整数値をテキストビューに設定しようとしています。あなたはこの

lblAvgDisp.setText(Average); 

この

lblAvgDisp.setText(String.valueOf(Average)); 
関連する問題