program tip

android.content.res.Resources $ NotFoundException : 메인의 문자열 리소스 ID 치명적인 예외

radiobox 2020. 11. 29. 10:10
반응형

android.content.res.Resources $ NotFoundException : 메인의 문자열 리소스 ID 치명적인 예외


나는 작은 난수를 가져 와서 사용자에게 텍스트 뷰로 표시하는 간단한 프로그램을 만들려고 노력해 왔습니다. 마침내 생성 할 난수를 얻은 후 (내 생각에) 실행할 때마다 프로그램에서 치명적인 예외가 발생합니다. 코드에 오류는 없지만 나는 완전히 초보자이고 배울 수 있도록 간단하게 시작하고 있습니다. 몇 시간 후에 나는 도움 요청을 제출했습니다. 난수에 대한 내 스 니펫이 잘못된 영역에 있다고 거의 확신합니다. 어디에 넣을지 모르겠습니다. 내가 시도한 모든 곳에서 동일한 오류가 발생합니다.

이것은 .java입니다.

package com.eai.vgp;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

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

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;

    }


    Random pp = new Random();
int a1 = pp.nextInt(10);

TextView tv = (TextView)findViewById(R.id.tv);{

tv.setText(a1);}

    }

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

</RelativeLayout>

LogCat

03-03 16:34:25.313: I/Process(740): Sending signal. PID: 740 SIG: 9
03-03 16:35:02.212: E/Trace(806): error opening trace file: No such file or directory     (2)
03-03 16:35:02.802: W/ResourceType(806): No package identifier when getting value for     resource number 0x00000001
03-03 16:35:02.813: D/AndroidRuntime(806): Shutting down VM
03-03 16:35:02.813: W/dalvikvm(806): threadid=1: thread exiting with uncaught exception     (group=0x40a13300)
03-03 16:35:02.833: E/AndroidRuntime(806): FATAL EXCEPTION: main
03-03 16:35:02.833: E/AndroidRuntime(806): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.eai.vgp/com.eai.vgp.MainActivity}:     android.content.res.Resources$NotFoundException: String resource ID #0x1
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.ActivityThread.access$600(ActivityThread.java:130)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.os.Handler.dispatchMessage(Handler.java:99)
03-03 16:35:02.833: E/AndroidRuntime(806):  at android.os.Looper.loop(Looper.java:137)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.ActivityThread.main(ActivityThread.java:4745)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     java.lang.reflect.Method.invokeNative(Native Method)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     java.lang.reflect.Method.invoke(Method.java:511)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-03 16:35:02.833: E/AndroidRuntime(806):  at dalvik.system.NativeStart.main(Native     Method)
03-03 16:35:02.833: E/AndroidRuntime(806): Caused by:     android.content.res.Resources$NotFoundException: String resource ID #0x1
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.content.res.Resources.getText(Resources.java:229)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.widget.TextView.setText(TextView.java:3620)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     com.eai.vgp.MainActivity.onCreate(MainActivity.java:22)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.Activity.performCreate(Activity.java:5008)
03-03 16:35:02.833: E/AndroidRuntime(806):  at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-03 16:35:02.833: E/AndroidRuntime(806):      atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-03 16:35:02.833: E/AndroidRuntime(806):  ... 11 more
03-03 16:35:05.113: I/Process(806): Sending signal. PID: 806 SIG: 9

움직임

Random pp = new Random();
int a1 = pp.nextInt(10);
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(a1);

내부 onCreate()로 변경 tv.setText(a1);하고 tv.setText(String.valueOf(a1));다음으로 변경 하십시오 .

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

  Random pp = new Random();
  int a1 = pp.nextInt(10);

  TextView tv = (TextView)findViewById(R.id.tv);
  tv.setText(String.valueOf(a1));

}   

첫 번째 문제 : findViewById()이전에 호출 onCreate()되었으므로 NPE가 발생합니다.

Second issue: Passing an int directly to a TextView calls the overloaded method that looks for a String resource (from R.string). Therefore, we want to use String.valueOf() to force the String overloaded method.


You tried to do a.setText(a1). a1 is an int value, but setText() requires a string value. For this reason you need use String.valueOf(a1) to pass the value of a1 as a String and not as an int to a.setText(), like so:

a.setText(String.valueOf(a1))

that was the exact solution to the problem with my case.


tv.setText( a1 + " ");

This will resolve your problem.


You are trying to set int value to TextView so you are getting this issue. To solve this try below one option

option 1:

tv.setText(no+"");

Option2:

tv.setText(String.valueOf(no));

Other possible solution:

tv.setText(Integer.toString(a1));  // where a1 - int value

This always can happen in DataBinding. Try to stay away from adding logic in your bindings, including appending an empty string. You can make your own custom adapter, and use it multiple times.

@BindingAdapter("numericText")
fun numericText(textView: TextView, value: Number?) {
    value?.let {
        textView.text = value.toString()
    }
}

<TextView app:numericText="@{list.size()}" .../>

참고URL : https://stackoverflow.com/questions/15191092/android-content-res-resourcesnotfoundexception-string-resource-id-fatal-except

반응형