Convertir ImageView a entero: Android Studio

Nathan:

Estoy tratando de tomar el destino (iniciada como ImageView) Identificación y poner el id entero en un caso de cambio a vistazo a las adyacentes Vistas y comparar sus dibujables para determinar si ese jugador gana o si el juego continúa. Tengo la variable buttonPressed iniciado como un entero y se utiliza la parseInt () para obtener el valor int del objetivo.

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;

No estoy llenando todos los casos con fines de depuración.

El problema que estoy teniendo es cuando corro el emulador me sale un error que dice

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)

Aquí está el código para el 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();

    }

No del todo seguro de lo que está pasando en este momento porque creo que lo hice todo correcto pero es evidente que algo se ha perdido. Cualquier ayuda sería apreciada.

khushboo:

cambio :

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

A :

buttonPressed = target.getId ();

Explicación: el error dice NumberFormatException medios que están tratando de obtener int valor de cadena que no es posible analizar o en su sencilla cadena no contiene valor int adecuada y también está de paso (androidx.appcompat.widget ...) como cuerdas mientras tiene botón que había que pasar

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=360932&siteId=1
Recomendado
Clasificación