【问题标题】:How to fix cannot convert from int[] to int如何修复无法从 int[] 转换为 int
【发布时间】:2015-08-10 18:41:32
【问题描述】:

我的项目中有以下代码

ImageView lifesimage, good;
TextView lifes1;

int gamelifes=6, gamehints=6, index=0; 
int [] heartimage={R.drawable.lifessix,
                   R.drawable.lifesfive,
                   R.drawable.lifesfour,
                   R.drawable.lifesthree,
                   R.drawable.lifestwo,
                   R.drawable.lifesone,
                   R.drawable.lifesno,
                   };

    good=(ImageView)findViewById(R.id.youwin);

    lifesimage =(ImageView)findViewById(R.id.lifes);
    lifesimage.setImageResource(heartimage[0]);

    lifes1 =(TextView)findViewById(R.id.lifestext);
    lifes1.setTextColor(Color.RED);
    lifes1.setText(String.valueOf(gamelifes));

所以如果答案正确或错误那么代码是

 if(you.equalsIgnoreCase(answer[index]))
            {                   
                good.setVisibility(View.VISIBLE);
            }
            else
            {    
                heartimage++;                    
                gamelifes--;
                lifes1.setText(String.valueOf(gamelifes));
            }

但我在heartimage++; 上发现了一些错误,例如类型不匹配:无法从 int[] 转换为 int 如何修复此代码?有没有可以让图片出现顺序的功能?

【问题讨论】:

  • 你必须使用一个count变量来增加int的值。就像 int n; n++;心脏图像[n]
  • 它不是 C 并且 heartimage 不是指针 ...
  • @Deepak Goyal 你的意思是像索引的功能?
  • @Ricci 是的,就像索引的功能。

标签: android image int


【解决方案1】:
您的代码中的

'heartimage' 是一个整数数组,而您正试图将 1 加到它... 不起作用。创建一个新变量“索引”(或任何您想命名的名称),它将存储您想要的心脏值的当前索引。

然后是 lifesimage.setImageResource(heartimage[index]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    相关资源
    最近更新 更多