2017-12-06 6 views
-1

안드로이드 프로젝트의 android values ​​/ strings.xml 파일에 몇 개의 문자열이 있습니다.

<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> 

나는 나의 자바 클래스

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

첫 번째 줄은 잘 실행하지만, 두 번째 줄이 오류 (프로그램이 주석 라인 2와 함께 작동) 제공 한이 라인 문자열을 호출 :

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 줄에 여러 문자열을 사용하려고하기 때문에 그것이 있습니까? 왜 다른 문자열은 잘로드됩니까?

참고 : 문제가 R.string.xxx 반환 문자열의 ID입니다 그래서 기본적으로

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

R.string.endgame_textP1 문자열이 아니라 더 나은 – Selvin

답변

0

좋아요 : 세트가되고있는 두 개의 개체가 TextView 년대이다.

내 문자열에 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