2017-09-20 1 views
0

내 앱이 API 23 이상에서 잘 실행되고 있지만 API 21에서 실행할 때 버그가있는 것으로 보입니다.이 코드의 일부로 범위를 좁혔습니다. :API의 리소스 비교 21 x API 23

항상 반환 API (21)에
if (myButton.getBackground().getConstantState() == ResourcesCompat.getDrawable(getResources(), R.drawable.myDrawable, null).getConstantState()) 
{...} 

거짓 (그것은 안 경우에도), 그래서 나는 그 방법 중 하나는 API (21)

어떤 아이디어에서 작동하지 않는 것 같아요? 버튼의 배경에 특정 드로어 블이 지정되어 있는지 확인하는 다른 방법 (API 21 이상과 호환)이 있습니까?

ps : 내 minsdk는 21이지만 Lint가 경고를 보내지 않습니다.

답변

1

당신은 ContextCompat를 사용하여 시도 할 수 있습니다 :

ContextCompat.getDrawable(yourContext, R.drawable.myDrawable).getConstantState(); 
+0

그것은했다. 왜 그런지 알고 있니? 도와 주셔서 감사합니다! –

+0

https://developer.android.com/reference/android/support/v4/content/res/ResourcesCompat.html#getDrawable(android.content.res.)에서 ResourceCompat의 문제점을 확인할 수 없습니다. Resources, int, android.content.res.Resources.Theme)은'getDrawable (resource)'와 동일하다. 하지만'ResourcesCompat.getDrawable (getResources(), R.drawable.myDrawable, null) .getConstantState()'를 사용하면 Android Studio는 null을 반환 할 수 있다고 불평 할 것입니다. 따라서 문제가 발생합니다. –