的getContext的getResource和5.0,下之间的兼容性 [英] Compatibility of getContext and getResource between 5.0 and lower

查看:263
本文介绍了的getContext的getResource和5.0,下之间的兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我发表我的应用程序在几个月前,这一切工作正常,并在一定程度上它仍然如此。在运行比Android设备的低5一切都很好。但是我今天测试了5设备上,我的if语句不工作。例如它标志着每一个答案是不正确的但较低的设备的正确不正确的答案是如预期的。我知道我的code ++工程,但我不明白为什么它没有在Android 5.工作我唯一能想到的是,我已经错过了一些东西在我的清单。

编辑:工作ANSWER允许之间的兼容性和LT = 4.0和5.0 =<:

  button.setOnClickListener(新OnClickListener(){

                    @TargetApi(Build.VERSION_ codeS.LOLLIPOP)@Override
                    公共无效的onClick(查看为arg0){

    如果(Build.VERSION.SDK_INT< Build.VERSION_ codeS.LOLLIPOP){
                            如果(Word1.getDrawable()。getConstantState()。等于(Word1.getResources()。getDrawable(R.drawable.img1).getConstantState())}}

    如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.LOLLIPOP){
                             如果(。Word1.getDrawable()getConstantState()等于(Word1.getContext()getDrawable(R.drawable.img1).getConstantState())}}});
 

解决方案

您需要使用 .equals 进行比较的对象。

== 检查,如果是同一个对象,如果没有对象保持相同的值,更多信息<一个href="http://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator">here

您code 768,16如下:

 如果(Word1.getDrawable()。getConstantState()。等于(getResources()。getDrawable(R.drawable.img9).getConstantState())
 

Hi so I released my app a few months ago and it all was working fine and to a degree it still does. On devices running lower than android 5 everything is fine. But I tested on a 5 device today and my if statements are not working. For example it marks every answer as incorrect yet on a lower device the correct incorrect answers is working as intended. I know my code works but I can't work out why it doesn't work on android 5. The only thing I can think of is that i've missed something out on my manifest.

EDIT: WORKING ANSWER allows compatibility between <=4.0 and 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())}}  }); 

解决方案

You need to use .equals to compare the objects.

== check if it's the same object not if the objects hold the same value, more info here

Your code shoud look like:

if(Word1.getDrawable().getConstantState().equals(getResources().getDrawable( R.drawable.img9).getConstantState())

这篇关于的getContext的getResource和5.0,下之间的兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆