Android : android.content.res.Resources $ NotFoundException : 문자열 리소스 ID # 0x5
앱을 실행할 때 제목에서 예외가 발생합니다. 행맨 게임에 대한 단어가 포함 된 .txt 파일이 있으며 파일에 액세스 할 때 예외가 발생한다고 생각합니다. 내 파일 cuvinte.txt는 / assets /에 있습니다. 내 코드는 다음과 같습니다 (레이아웃 / XML 부분을 건너 뛰었습니다).
public void onCreate() {
// all the onCreate() stuff, then this:
try {
AssetManager am = this.getAssets();
InputStream is = am.open("cuvinte.txt");
InputStreamReader inputStreamReader = new InputStreamReader(is);
BufferedReader b = new BufferedReader(inputStreamReader);
String rand;
while((rand=b.readLine())!=null){
cuvinte.add(rand);
}
} catch (IOException e) {
Toast.makeText(this, "No words file", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
newGame(newG);
}
public void newGame(View view){
Random rand = new Random();
String stringCuvant = cuvinte.get(rand.nextInt(cuvinte.size()));
cuvant.setText("");
System.out.println(stringCuvant);
for(int i = 0; i< stringCuvant.length(); i++){
cuvant.append("_ ");
}
incercari.setText(valIncercari);
}
newGame () 함수는 onCreate () 함수에서 새 게임 버튼을 누르고 활동 시작시 호출됩니다.
(단지 가정, 예외 스택 추적 정보가 적음)
나는,이 라인은, 생각 incercari.setText(valIncercari);
하기 때문에 예외를 throw valIncercari
이다int
그러니까
incercari.setText(valIncercari+"");
또는
incercari.setText(Integer.toString(valIncercari));
이 오류가 발생할 수있는 또 다른 이유를 지적하고 싶을 때 앱의 한 번역에 문자열 리소스를 정의했지만 기본 문자열 리소스를 제공하지 않았기 때문입니다.
문제의 예 :
아래에서 볼 수 있듯이 스페인어 문자열 "get_started"에 대한 문자열 리소스가 있습니다. 코드에서 여전히 참조 할 수 있지만 전화가 스페인어가 아닌 경우을 호출 할 때로드 및 충돌 할 리소스가 없습니다 getString()
.
values-es / strings.xml
<string name="get_started">SIGUIENTE</string>
자원에 대한 참조
textView.setText(getString(R.string.get_started)
로그 캣 :
06-11 11:46:37.835 7007-7007/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.app.test PID: 7007
android.content.res.Resources$NotFoundException: String resource ID #0x7f0700fd
at android.content.res.Resources.getText(Resources.java:299)
at android.content.res.Resources.getString(Resources.java:385)
at com.juvomobileinc.tigousa.ui.signin.SignInFragment$4.onClick(SignInFragment.java:188)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
문제 해결
이것을 방지하는 것은 매우 간단 values/strings.xml
합니다. 전화기에 다른 언어가 있으면 항상 기본 리소스가 포함 되어 있는지 확인하십시오.
values / strings.xml
<string name="get_started">Get Started</string>
values-ko / strings.xml
<string name="get_started">Get Started</string>
values-es / strings.xml
<string name="get_started">Siguiente</string>
values-de / strings.xml
<string name="get_started">Ioslegen</string>
이 예외를 발생시킬 수있는 또 다른 시나리오는 DataBinding을 사용하는 것입니다. 즉, 레이아웃에서 이와 같은 것을 사용할 때
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="model"
type="point.to.your.model"/>
</data>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@{model.someIntegerVariable}"/>
</layout>
Notice that the variable I'm using is an Integer and I'm assigning it to the text field of the TextView. Since the TextView already has a method with signature of setText(int)
it will use this method instead of using the setText(String)
and cast the value. Thus the TextView thinks of your input number as a resource value which obviously is not valid.
Solution is to cast your int value to string like this
android:text="@{String.valueOf(model.someIntegerVariable)}"
This problem mostly occurs due to the error in setText()
method
Solution is simple put your Integer
value by converting into string
type as
textview.setText(Integer.toString(integer_value));
Sometime this happened due to not fond any source like if i want to set a text into a textview from adapter then i should use
textView.setText(""+name);
If you write something like
textView.setText(name);
this will not work and sometime we don't find the resource from the string.xml file then this type of error occur.
Using DataBinding and setting background to the edittext with resources from the drawable folder causes the exception.
<EditText
android:background="@drawable/rectangle"
android:imeOptions="flagNoExtractUi"
android:layout_width="match_parent"
android:layout_height="45dp"
android:hint="Enter Your Name"
android:gravity="center"
android:textColorHint="@color/hintColor"
android:singleLine="true"
android:id="@+id/etName"
android:inputType="textCapWords"
android:text="@={viewModel.model.name}"
android:fontFamily="@font/avenir_roman"/>
Solution
I just change the background from android:background="@drawable/rectangle"
to android:background="@null"
Clean and Rebuild the Project.
'programing tip' 카테고리의 다른 글
변수가 범위 내에 있는지 확인? (0) | 2020.06.30 |
---|---|
SQLAlchemy 기본 DateTime (0) | 2020.06.30 |
jQuery를 사용하여 자식 요소를 부모에서 다른 부모 요소로 옮기는 방법 (0) | 2020.06.30 |
NSInteger를 NSString 데이터 유형으로 어떻게 변환합니까? (0) | 2020.06.30 |
'new'를 사용하면 왜 메모리 누수가 발생합니까? (0) | 2020.06.30 |