将ImageView转换为整数:Android Studio

问题描述 投票:0回答:1

我正在尝试获取目标ID(初始为ImageView)ID,并将整数ID放入开关盒中,以查看相邻的View,并比较其可绘制对象以确定该玩家是否获胜或游戏是否继续。我将buttonPressed变量初始化为整数,并使用parseInt()获取目标的int值。

public void compareButton(int buttonPressed){
    //int count = 0;
    ImageView adjacent;
    ImageView adjacentB;

    switch (buttonPressed){
        case R.id.imageButtonA: //this is where adjacent buttons are identified and compared
            adjacent = findViewById(R.id.imageButtonB);
            adjacentB = findViewById(R.id.imageButtonC);
            if (target.getDrawable() == adjacent.getDrawable() && target.getDrawable() == adjacentB.getDrawable()) {

                Toast.makeText(MainActivity.this, "You Win!", Toast.LENGTH_SHORT).show(); //Win condition


           // } else if (target.getDrawable() == R.id.imageButtonE.getDrawable() & target.getDrawable() == R.id.imageButtonI.getDrawable()) {

                //Toast.makeText(MainActivity.this, "You Win!", Toast.LENGTH_SHORT).show(); //Win condition

           // } else if (target.getDrawable() == R.id.imageButtonD.getDrawable() & target.getDrawable() == R.id.imageButtonG.getDrawable()) {

                //Toast.makeText(MainActivity.this, "You Win!", Toast.LENGTH_SHORT).show(); //Win condition

            }
            break;
        case R.id.imageButtonB:

            break;

我不是为了调试目的而填写每种情况。

我遇到的问题是,当我运行仿真器时,出现错误提示:>

Caused by: java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatImageButton{517eade VFED..C.. ...P..ID 45,381-304,628 #7f070072 app:id/imageButtonA}"
    at java.lang.Integer.parseInt(Integer.java:521)
    at java.lang.Integer.parseInt(Integer.java:556)
    at com.example.connect3.MainActivity.switchColor(MainActivity.java:75)

这里是OnClickListener的代码:

public void switchColor(View view) {

    //Button pressed, depending on user, switch's to that users color; identify adjacent button ID's; toast player control switch
    if (player == 1) {

        source = findViewById(R.id.yellow);
        target = findViewById(view.getId());
        target.setImageDrawable(source.getDrawable());
        buttonPressed = Integer.parseInt(target.toString());
        compareButton(buttonPressed);
        player++;
        Toast.makeText(MainActivity.this, "Player 2's Turn!", Toast.LENGTH_SHORT).show();

    } else {

        source = findViewById(R.id.red);
        target = findViewById(view.getId());
        target.setImageDrawable(source.getDrawable());
        buttonPressed = Integer.parseInt(String.valueOf(target));
        compareButton(buttonPressed);
        player--;
        Toast.makeText(MainActivity.this, "Player 1's Turn!", Toast.LENGTH_SHORT).show();

    }

不确定目前发生了什么,因为我以为我所做的一切都正确,但显然遗漏了一些东西。任何帮助,将不胜感激。

我正在尝试获取目标ID(作为ImageView发起),并将整数ID放入切换盒中,以查看相邻的View,并比较其可绘制对象以确定该玩家是否获胜或......>]] >

change:

buttonPressed = Integer.parseInt(String.valueOf(target));

收件人:

buttonPressed = target.getId();

说明:您的错误说NumberFormatException表示您试图从String中获取int值,而这是无法解析的,或者简单地,您的字符串不包含正确的int值,并且您正在传递(androidx.appcompat.widget ... )作为字符串,而您必须通过按钮我会>

java android integer imageview parseint
1个回答
0
投票

change:

© www.soinside.com 2019 - 2024. All rights reserved.