본문 바로가기
안드로이드

IllegalStateException: No instrumentation registered!

by Banlim 2020. 11. 19.

[삽질의 기록]

 

 

현재 진행하고있는 프로젝트에서 RecyclerView를 사용한다. RecyclerView는 RecyclerViewAdapter와 함께 사용해야한다. 이 때 Adapte에서 Toast를 사용해야했는데, 이 때 위와 같이 

 

IllegalStateException : No instrumentation registered! Must run under a registering instrumentation.

 

이러한 Exception을 만나게 되었다.

 

Toast는 Toast.makeText(context, message, duration).show(); 와 같은 형태로 사용할 수 있다.

이 때 context 자리에 getApplicationContext()를 사용하면 안된다.

Adapter에서 강제로 static 형태를 통해 getApplicationContext()를 사용하여 등록된 context를 찾을 수 없어 생기는 문제이다. 

따라서, 생성자에서 Context 필드를 추가하여 RecyclerView에서 adapter를 setting할 때 RecyclerView가 속해있는 Activity나 Fragment에서 Context를 가져와 이 Context를 사용하면 문제가 해결된다.