Notice
Recent Posts
Recent Comments
Link
투케이2K
296. (TWOK/ERROR) [Android] 안드로이드 android.content.Context.getResources() on a null object reference 본문
투케이2K 에러관리
296. (TWOK/ERROR) [Android] 안드로이드 android.content.Context.getResources() on a null object reference
투케이2K 2025. 2. 20. 20:09[환경 설정 및 설명]
프로그램 : AndroidStudio
설 명 : [Android] 안드로이드 android.content.Context.getResources() on a null object reference
[설 명]
--------------------------------------------------------------------------
[에러 원인]
--------------------------------------------------------------------------
1. 소스 코드 전역 변수쪽에 getResources 코드가 작성 되어 있어서 화면 접속 시 null object 반환 이슈
2. 에러 발생 소스 부분 : 전역 선언
String array[] = getResources().getStringArray(R.array.num_type);
3. 에러 발생 로그 :
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:129)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:134)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:128)
at android.widget.ArrayAdapter.createFromResource(ArrayAdapter.java:517)
--------------------------------------------------------------------------
--------------------------------------------------------------------------
[해결 방법]
--------------------------------------------------------------------------
1. 전역에 작성 된 getResources 호출 부분을 라이프 사이클 onCreate 내에서 호출 하도록 코드 변경
2. 변경 소스 코드 :
// [전역 변수]
String array[] = null;
// [onCreate 내 getResources 호출]
array = getResources().getStringArray(R.array.num_type);
--------------------------------------------------------------------------
--------------------------------------------------------------------------
[참고 사이트]
--------------------------------------------------------------------------
[ArrayAdapter.createFromResource 사용해 스피너 메뉴 목록에 xml resource string array 지정]
https://blog.naver.com/kkh0977/223240345738?trackingCode=blog_bloghome_searchlist
--------------------------------------------------------------------------
반응형
'투케이2K 에러관리' 카테고리의 다른 글
Comments