greenhelix
greenhelix
greenhelix
05-13 02:06
  • All (229)
    • Algorithm (118)
      • Algorithm (17)
      • Graph (0)
      • Core (6)
      • Python (18)
      • PythonSnippet (4)
      • Java (59)
      • Kotlin (14)
    • Project (0)
    • Study (8)
      • License (5)
      • EIP (3)
    • Programming (63)
      • Android (41)
      • Flutter (1)
      • Bugs Life (21)
      • Linux (0)
    • Tech (32)
      • Tech (17)
      • Drone (4)
      • Hacking (11)
    • Life (6)
      • INGRESS (1)
      • 심시티빌드잇 (0)
250x250

티스토리

hELLO · Designed By 정상우.
greenhelix

greenhelix

[Android] Caused by: java.lang.NullPointerException
Programming/Bugs Life

[Android] Caused by: java.lang.NullPointerException

2020. 10. 21. 18:06

오류:

 

Caused by: java.lang.NullPointerException:Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference

위의 오류는 ImageButton, ImageView 를 사용하려하는데, 이상하게 id를 평상시처럼 입력해도 인식을 못한다고 에러가 뜨는 것이었다. 

보통 xml의 객체 값을 activity에서 다시 선언해주고 할당을 해줘야한다. 

 

ImageButton a ; 

onCreate()-->

      a.findViewById(R.id.btn_image); 

이런식으로 선언을 해주면 되는데, 이상하게 imageView와 imageButton이 안되는 현상이 발생. 

 

해결책:

선언부분의 형태를 형변환? 처럼해줘야 하나보다. 

 

a = (ImageButton) findViewById(R.id.btn_image);

 

이럭식으로 변형을 주니 정상 가동되더라. 

 


StackOverFlow 참고 

바로가기

728x90
반응형
저작자표시 비영리 변경금지

'Programming > Bugs Life' 카테고리의 다른 글

[Android] Invalid VCS root mapping ERROR Trouble Shooting Error  (0) 2020.10.28
[Android] Firebase & gradle 버전 충돌  (0) 2020.10.20
[Android] Button 위치 에러 :: [This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints.]  (0) 2020.10.09
    'Programming/Bugs Life' 카테고리의 다른 글
    • [Android] java.lang.NullPointerException: Attempt to invoke virtual method ' jav
    • [Android] Invalid VCS root mapping ERROR Trouble Shooting Error
    • [Android] Firebase & gradle 버전 충돌
    • [Android] Button 위치 에러 :: [This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints.]
    greenhelix
    greenhelix
    개발에 관한 것들과 개인적인 것을 담는 블로그

    티스토리툴바