🐛 버그
[Bug / Android] requireContext IllegalStateException
콩드로이드
2024. 8. 7. 10:22
🐛 requireContext IllegalStateException
requireContext()의 원형을 살펴보면
/**
* Return the {@link Context} this fragment is currently associated with.
*
* @throws IllegalStateException if not currently associated with a context.
* @see #getContext()
*/
@NonNull
public final Context requireContext() {
Context context = getContext();
if (context == null) {
throw new IllegalStateException("Fragment " + this + " not attached to a context.");
}
return context;
}
@NonNull이기 때문에, null이면 IllegalStateException이 발생합니다.
만약 Fragment가 Activity에 연결되어 있지 않거나, Fragment가 이미 detach된 상태에서 호출될 때 발생하기에
🔑 non-null 체크 후에 사용하기 혹은 getContext 사용하기