java - 将 ImageView 转换为整数 : Android Studio

标签 java android integer imageview parseint

我试图获取目标(启动为 ImageView)id 并将整数 id 放入 switch case 中以查看相邻的 View 并比较它们的可绘制对象以确定该玩家是否获胜或游戏是否继续。我将 buttonPressed 变量初始化为 Integer,并使用 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();

    }

不完全确定此时发生了什么,因为我认为我做的一切都是正确的,但显然遗漏了一些东西。任何帮助将不胜感激。

最佳答案

更改:

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

致:

buttonPressed = target.getId();

解释:您的错误表示 NumberFormatException 意味着您正在尝试从无法解析的 String 获取 int 值,或者简单来说您的字符串不包含正确的 int 值,并且您正在传递 (androidx.appcompat.widget.. .) 作为字符串,而你必须传递按钮我会

关于java - 将 ImageView 转换为整数 : Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60907270/

相关文章:

android - Broadcastreceiver组件不允许绑定(bind)到android中的服务

android - ByteArray Property Too Large to send within putExtra ("obj", obj) of intent 打开另一个 Activity 时

java - 将字符(数字)转换为整数的 "java"方式是什么

php - 可以将一个变量乘以一个变量来检查它是否真的是 php 中的一个数字吗?

java - 2014 年 BroadcastReceiver 的 Google Play/Google Analytics INSTALL_REFERRER

java - 是否可以在 SoftHSM 上存储 key ?

java - 从 URL 下载文件到文件夹

java - 检查数字的二进制表示形式中所有设置位后仅跟随未设置位的最佳方法

java - Android游戏开发-BOT玩家线程的使用和AndEngine配置

java - 整数除法及其赋值