Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 比较API 21 x API 23中的资源_Android_Android 5.0 Lollipop_Android Resources - Fatal编程技术网

Android 比较API 21 x API 23中的资源

Android 比较API 21 x API 23中的资源,android,android-5.0-lollipop,android-resources,Android,Android 5.0 Lollipop,Android Resources,我的应用程序在API 23及以上版本中运行良好,但在API 21中运行时,它似乎有一个bug。我已将其缩小为以下代码: if (myButton.getBackground().getConstantState() == ResourcesCompat.getDrawable(getResources(), R.drawable.myDrawable, null).getConstantState()) {...} 在API21中,它总是返回false(即使它不应该返回false),所以我猜其

我的应用程序在API 23及以上版本中运行良好,但在API 21中运行时,它似乎有一个bug。我已将其缩小为以下代码:

if (myButton.getBackground().getConstantState() == ResourcesCompat.getDrawable(getResources(), R.drawable.myDrawable, null).getConstantState())
{...}
在API21中,它总是返回false(即使它不应该返回false),所以我猜其中一个方法在API21中不起作用

有什么想法吗?是否有其他方法(与API 21及以上兼容)来检查按钮的背景是否指定了特定的绘图


ps:我的minsdk是21,但Lint没有给我任何警告。

你可以尝试使用ContextCompat:

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

成功了。你知道为什么吗?谢谢你的帮助!我不能确定ResourceCompat的问题是什么,因为从,int,android.content.res.Resources.Theme)中,使用null将与
getDrawable(resource)
相同。但是当我们使用
ResourcesCompat.getDrawable(getResources(),R.drawable.myDrawable,null)时,getConstantState()
Android Studio会抱怨它可以返回null。于是问题就出现了。