简体   繁体   English

将 ImageView 转换为整数:Android Studio

[英]Convert ImageView to Integer: Android Studio

I'm trying to take the target (initiated as ImageView) id and put the integer id into a switch case to look at the adjacent Views and compare their drawables to determine if that player wins or if the game continues.我正在尝试获取目标(启动为 ImageView)id 并将整数 id 放入 switch case 以查看相邻的 Views 并比较它们的 drawable 以确定该玩家是否获胜或游戏是否继续。 I have the buttonPressed variable initiated as an Integer and used the parseInt() to get the int value of target.我将 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;

I am not filling every case for debugging purposes.我不是为了调试目的而填写每个案例。

The issue I am having is when I run the emulator I get an error that says我遇到的问题是当我运行模拟器时,我收到一个错误消息

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)

Here is the code for the OnClickListener:这是 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();

    }

Not entirely sure what is going on at this point because I thought I did everything correct but clearly something was missed.不完全确定此时发生了什么,因为我认为我做的一切都是正确的,但显然遗漏了一些东西。 Any help would be appreciated.任何帮助,将不胜感激。

change :改变 :

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

To :到 :

buttonPressed = target.getId(); buttonPressed = target.getId();

Explanation : your error says NumberFormatException means you are trying to get int value from String which is not possible to Parse or in simple your string doesn't contain proper int value and also you are passing (androidx.appcompat.widget...) as string while you have to pass button I'd说明:您的错误表示 NumberFormatException 意味着您正在尝试从无法解析的 String 获取 int 值,或者简单地说,您的字符串不包含正确的 int 值,并且您正在传递 (androidx.appcompat.widget...) 作为字符串,而你必须通过按钮我会

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM