getContext和getResource在5.0和更低版本之间的兼容性

时间:2014-12-13 21:19:19

标签: android xml if-statement android-manifest

大家好,所以我几个月前发布了我的应用程序,这一切都很好,在某种程度上它仍然可以。在运行低于android 5的设备上,一切都很好。但我今天在5台设备上进行了测试,我的if语句无法正常工作。例如,它将每个答案标记为不正确,但在较低的设备上,正确的错误答案按预期工作。我知道我的代码有效,但我无法解决为什么它在android 5上不起作用。我能想到的唯一一件事是我错过了我的清单。

编辑:工作答案允许< = 4.0和5.0 =<:

之间的兼容性
 button.setOnClickListener(new OnClickListener(){

                    @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override
                    public void onClick(View arg0) {

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                            if (Word1.getDrawable().getConstantState().equals(Word1.getResources().getDrawable( R.drawable.img1).getConstantState())}}

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {        
                             if  (Word1.getDrawable().getConstantState().equals(Word1.getContext().getDrawable( R.drawable.img1).getConstantState())}}  }); 

1 个答案:

答案 0 :(得分:2)

您需要使用.equals来比较对象。

==检查它是否是同一个对象,如果对象保持相同的值,则更多信息here

你的代码应该是:

if(Word1.getDrawable().getConstantState().equals(getResources().getDrawable( R.drawable.img9).getConstantState())
相关问题