2017-12-06 6 views
-1

私のアンドロイドプロジェクトのアンドロイドの値/ strings.xmlファイルに入れられた文字列があります。私は(プログラムがコメントアウト第二ラインで動作します)最初の行が正常に動作が、2番目の行は、このエラーを与える私のJavaクラスの1

endLevelLabel.setText(R.string.level_reached_label + level); 
endInfoLabel.setText(R.string.endgame_textP1 + score + R.string.endgame_textP2 + gameTime + R.string.endgame_textP3); 

にこの行と列を呼び出す

<resources> 
    <string name="level_reached_label">Level Reached: </string> 
    <string name="endgame_textP1">You have scored </string> 
    <string name="endgame_textP2"> points in </string> 
    <string name="endgame_textP3"> seconds </string> 
</resources> 

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.web.mathtaketwo/com.web.mathtaketwo.EndGame}: android.content.res.Resources$NotFoundException: String resource ID #0x7d1500be 

この文字列が動作しなくなった原因は何でしたか?私は変数と組み合わせて1行に複数の文字列を使用しようとしているからですか?なぜ他の文字列はうまく読み込まれますか?

注:設定されている2つのオブジェクトがTextView「sである:

TextView endInfoLabel = (TextView)findViewById(R.id.endInfoLabel); 
TextView endLevelLabel = (TextView)findViewById(R.id.endLevelLabel); 
+2

R.string.endgame_textP1は、文字列ではなく、より良い – Selvin

答えて

0

さてので、基本的な問題は、文字列の唯一ID R.string.xxx戻ります。

IDの(整数)を文字列に挿入しようとすると、多くの問題が発生します。私はgetString()を使用して自分のコードを変更した場合は

getString(R.string.xxxx) 

endLevelLabel.setText(getString(R.string.level_reached_label) + (level)); 
endInfoLabel.setText(getString(R.string.endgame_textP1) + score + getString(R.string.endgame_textP2) + gameTime + getString(R.string.endgame_textP3)); 

を次にそれが正常に動作します私が行っている必要があります代わりに何

は、指定されたIDで文字列を返しますgetString()機能を使用しました。詳細は、私が見て、これらの他のトピックを参照してください:

Reading value from string resource

String Resources

Get string from resource

+0

またはint型:形式でのgetStringを使用します – Selvin

関連する問題