所以我发表我的应用程序在几个月前,这一切工作正常,并在一定程度上它仍然如此。在运行比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
进行比较的对象。
==
检查,如果是同一个对象,如果没有对象保持相同的值,更多信息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())