Android studio (Android Studio)在我的GridView中单击时的某些位置不';我不能在非常特殊的情况下工作

Android studio (Android Studio)在我的GridView中单击时的某些位置不';我不能在非常特殊的情况下工作,android-studio,gridview,position,Android Studio,Gridview,Position,好吧,我搞不懂这个错误。它非常具体,我在网上找不到任何帮助。我正在Android Studio中用GridView和ImageAdapter制作一个跳棋游戏。我的代码对大多数人来说可能是一团糟,但希望有人能读懂它并找出我的问题 问题是我的棋盘格不会移动到棋盘上的某些位置。此图像中的绿点是它拒绝移动到的点: OnItemClickListener中的所有内容都是棋盘游戏逻辑,它通过重置ImageAdapter重新绘制棋盘 编辑:我被要求将代码缩减到我认为问题所在的地方,但我不完全确定它到底在哪里。

好吧,我搞不懂这个错误。它非常具体,我在网上找不到任何帮助。我正在Android Studio中用
GridView
ImageAdapter
制作一个跳棋游戏。我的代码对大多数人来说可能是一团糟,但希望有人能读懂它并找出我的问题

问题是我的棋盘格不会移动到棋盘上的某些位置。此图像中的绿点是它拒绝移动到的点:

OnItemClickListener
中的所有内容都是棋盘游戏逻辑,它通过重置
ImageAdapter
重新绘制棋盘

编辑:我被要求将代码缩减到我认为问题所在的地方,但我不完全确定它到底在哪里。问题在于,当其中一个突出显示的部分及其周围环境无法移动到如上图所示的突出显示的瓷砖中时。我真的相信这是一个问题,它不是网格没有检测到什么项目已点击,虽然我可以点击任何其他瓷砖取消选择所选的一块。我将剪掉移动部分,因为它可能就在这里,但我也将保留它下面的所有其余代码

此处是移动工件代码,当尝试将工件移动到以下位置的选定绿色点时,该代码将不起作用:


/reddit上的u/anomie-p:

我还没有做过很多android开发,但是你能不能连接一个调试器,设置一个断点并逐步执行它

此外,您可能希望使游戏逻辑成为自己的东西,它可以代表游戏并独立于用户界面执行游戏操作,然后将用户界面连接到该界面上进行调用。这会让你做一些事情,比如旋转一个游戏的表现和单元测试。当你想要的时候,它还可以让你更容易地翻转AI的哪一面。现在,用户界面代码会跟踪轮到谁了

你也有很多重复你自己的话。我是说,我想你知道你需要清理,所以也许我应该停止说话

今天早上我有一些轻松的东西要开始工作,但也许在那之后,我会把android的东西拉下来,然后再加上一点——但你肯定应该用调试器和断点来运行它,然后找出哪里出了问题

~~你能告诉我你的绘图工具需要什么xml/什么吗?这对我来说既简单又方便——我已经在android studio中加载了这些内容,我想我可以创建缺少的可绘制定义,但仅仅拥有你已经得到的似乎更简单/更不容易出错~~

我只是做了一些看起来很有效的

问题是:您正在计算位置数组之外的索引,并试图访问它们。因此运行时抛出ArrayIndexOutOfBoundsException,应用程序崩溃

表达方式如下: (位置[位置-18]==5 | |位置[位置-18]==9)和&(位置[位置-9]==4 | | |位置[位置-7]==8)和位置[位置]==2

将为小于18或大于(placement.length-19)的任何位置提供索引

我的建议仍然是重写以不同的方式组织的东西,这样你的游戏逻辑就不会与UI如此紧密地联系在一起——但是如果你想继续这样做,可以做类似的事情

private boolean isPlaceEqual(int position, int offset, int value)
{
    int index = position + offset;
    if(index > -1 && index < placement.length)
    {
        return placement[index] == value;
    }

    return false;
}
private boolean isPlaceEqual(int位置、int偏移、int值)
{
int索引=位置+偏移量;
如果(索引>-1&&index
把你的条件改成
else if(isplaceeEqual(位置,-18,5)| isPlaceEqual(位置,18,9).


(并在检查放置数组的任何位置使用安全边界检查谓词,以确保值的一致性-如果没有偏移量,只需将其传递为零)

Hi Miro!你能把你的问题简化为一个吗?谢谢!好吧,我添加了一段可能存在问题的代码,尽管我不能100%确定这是否是问题的一部分。这是我面临的问题之一。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.widget.TextView;
import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

    int[] placement = {
                    1, 3, 1, 3, 1, 3, 1, 3,
                    3, 1, 3, 1, 3, 1, 3, 1,
                    1, 3, 1, 3, 1, 3, 1, 3,
                    0, 1, 0, 1, 0, 1, 0, 1,
                    1, 0, 1, 0, 1, 0, 1, 0,
                    4, 1, 4, 1, 4, 1, 4, 1,
                    1, 4, 1, 4, 1, 4, 1, 4,
                    4, 1, 4, 1, 4, 1, 4, 1 };
    ImageView pic;
    ImageAdapter adapter = new ImageAdapter(this);
    int redScore = 0;
    int whiteScore = 0;
    boolean redTurn = true;
    boolean ai = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final GridView board = (GridView) findViewById(R.id.board);
        board.setAdapter(adapter);

        final TextView turnTeller = (TextView)findViewById(R.id.Turn);
        if(!ai)
            turnTeller.setText("Red's Turn");
        else
            turnTeller.setText("");

        board.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (redTurn) {
                    if (placement[position] != 2) //DESELECT PIECE
                    {
                        for (int i = 0; i < placement.length; i++) {
                            if (placement[i] == 2) {
                                placement[i] = 0;
                            } else if (placement[i] == 6) {
                                placement[i] = 4;
                            } else if (placement[i] == 10) {
                                placement[i] = 8;
                            }
                        }
                    }
                    if (placement[position] == 4 || placement[position] == 8) //SELECT PIECE
                    {
                        if (placement[position] == 4) {
                            placement[position] = 6;
                        } else {
                            placement[position] = 10;
                        }


                        if (position != 8 && position != 24 && position != 40 && position != 56 && position != 63 && position != 47 && position != 31 && position != 15) {
                            if (placement[position - 7] == 0) {
                                placement[position - 7] = 2;
                            }
                            if (placement[position - 9] == 0) {


     placement[position - 9] = 2;
                        }
                        if (placement[position - 7] == 3) {
                            if (placement[position - 14] == 0) {
                                placement[position - 14] = 2;
                            }
                        }
                        if (placement[position - 9] == 3) {
                            if (placement[position - 18] == 0) {
                                placement[position - 18] = 2;
                            }
                        }
                        if (placement[position] == 10) {
                            if (placement[position + 7] == 0) {
                                placement[position + 7] = 2;
                            }
                            if (placement[position + 9] == 0) {
                                placement[position + 9] = 2;
                            }
                            if (placement[position + 7] == 3) {
                                if (placement[position + 14] == 0) {
                                    placement[position + 14] = 2;
                                }
                            }
                            if (placement[position + 9] == 3) {
                                if (placement[position + 18] == 0) {
                                    placement[position + 18] = 2;
                                }
                            }
                        }
                    } else if (position == 8 || position == 24 || position == 40 || position == 56 || position == 63) {
                        if (placement[position - 7] == 0) {
                            placement[position - 7] = 2;
                        }
                        if (placement[position - 7] == 3) {
                            if (placement[position - 14] == 0) {
                                placement[position - 14] = 2;
                            }
                        }
                        if (placement[position] == 10) {
                            if (placement[position + 7] == 0) {
                                placement[position + 7] = 2;
                            }
                            if (placement[position + 7] == 3) {
                                if (placement[position + 14] == 0) {
                                    placement[position + 14] = 2;
                                }
                            }
                        }
                    } else {
                        if (placement[position - 9] == 0) {
                            placement[position - 9] = 2;
                        }
                        if (placement[position - 9] == 3) {
                            if (placement[position - 18] == 0) {
                                placement[position - 18] = 2;
                            }
                        }
                        if (placement[position] == 10) {
                            if (placement[position + 9] == 0) {
                                placement[position + 9] = 2;
                            }
                            if (placement[position + 9] == 3) {
                                if (placement[position + 18] == 0) {
                                    placement[position + 18] = 2;
                                }
                            }
                        }
                    }
                }
                if (placement[position] == 2) //MOVEMENT OF PIECE
                {
                    if ((placement[position + 14] == 6 || placement[position + 14] == 10) && (placement[position + 7] == 3 || placement[position + 7] == 7) && placement[position] == 2) {
                        placement[position + 7] = 0;
                        redScore++;
                        redTurn = false;
                        turnTeller.setText("White's Turn");
                    } else if ((placement[position + 18] == 6 || placement[position + 18] == 10) && (placement[position + 9] == 3 || placement[position + 9] == 7) && placement[position] == 2) {
                        placement[position + 9] = 0;
                        redScore++;
                        redTurn = false;
                        turnTeller.setText("White's Turn");
                    } else if (placement[position - 14] == 10 && (placement[position - 7] == 3 || placement[position + 7] == 7) && placement[position] == 2) {
                        placement[position - 7] = 0;
                        redScore++;
                        redTurn = false;
                        turnTeller.setText("White's Turn");
                    } else if (placement[position - 18] == 10 && (placement[position - 9] == 3 || placement[position - 9] == 7) && placement[position] == 2) {
                        placement[position - 9] = 0;
                        redScore++;
                        redTurn = false;
                        turnTeller.setText("White's Turn");
                    }
                    for (int i = 0; i < placement.length; i++) {
                        if (placement[i] == 6) {
                            placement[position] = 4;
                            placement[i] = 0;
                            redTurn = false;
                            turnTeller.setText("White's Turn");
                        } else if (placement[i] == 10) {
                            placement[position] = 8;
                            placement[i] = 0;
                            redTurn = false;
                            turnTeller.setText("White's Turn");
                        }
                        if (placement[i] == 2) {
                            placement[i] = 0;
                        }
                    }
                }

                if (placement[1] == 4) //KING RED
                {
                    placement[1] = 8;
                } else if (placement[3] == 4) {
                    placement[3] = 8;
                } else if (placement[5] == 4) {
                    placement[5] = 8;
                } else if (placement[7] == 4) {
                    placement[7] = 8;
                }
                board.setAdapter(adapter);
            }

            else if(ai == false)
            {
                if (placement[position] != 2) //DESELECT PIECE
                {
                    for (int i = 0; i < placement.length; i++) {
                        if (placement[i] == 2) {
                            placement[i] = 0;
                        } else if (placement[i] == 5) {
                            placement[i] = 3;
                        } else if (placement[i] == 9) {
                            placement[i] = 7;
                        }
                    }
                }
                if (placement[position] == 3 || placement[position] == 7) //SELECT PIECE
                {
                    if (placement[position] == 3) {
                        placement[position] = 5;
                    } else {
                        placement[position] = 9;
                    }


                    if (position != 8 && position != 24 && position != 40 && position != 56 && position != 63 && position != 47 && position != 31 && position != 15) {
                        if (placement[position + 7] == 0) {
                            placement[position + 7] = 2;
                        }
                        if (placement[position + 9] == 0) {
                            placement[position + 9] = 2;
                        }
                        if (placement[position + 7] == 4) {
                            if (placement[position + 14] == 0) {
                                placement[position + 14] = 2;
                            }
                        }
                        if (placement[position + 9] == 4) {
                            if (placement[position + 18] == 0) {
                                placement[position + 18] = 2;
                            }
                        }
                        if (placement[position] == 9) {
                            if (placement[position - 7] == 0) {
                                placement[position - 7] = 2;
                            }
                            if (placement[position - 9] == 0) {
                                placement[position - 9] = 2;
                            }
                            if (placement[position - 7] == 4) {
                                if (placement[position - 14] == 0) {
                                    placement[position - 14] = 2;
                                }
                            }
                            if (placement[position - 9] == 4) {
                                if (placement[position - 18] == 0) {
                                    placement[position - 18] = 2;
                                }
                            }
                        }
                    } else if (position == 8 || position == 24 || position == 40 || position == 56 || position == 63) {
                        if (placement[position + 7] == 0) {
                            placement[position + 7] = 2;
                        }
                        if (placement[position + 7] == 4) {
                            if (placement[position + 14] == 0) {
                                placement[position + 14] = 2;
                            }
                        }
                        if (placement[position] == 9) {
                            if (placement[position - 7] == 0) {
                                placement[position - 7] = 2;
                            }
                            if (placement[position - 7] == 4) {
                                if (placement[position - 14] == 0) {
                                    placement[position - 14] = 2;
                                }
                            }
                        }
                    } else {
                        if (placement[position + 9] == 0) {
                            placement[position + 9] = 2;
                        }
                        if (placement[position + 9] == 4) {
                            if (placement[position + 18] == 0) {
                                placement[position + 18] = 2;
                            }
                        }
                        if (placement[position] == 9) {
                            if (placement[position - 9] == 0) {
                                placement[position - 9] = 2;
                            }
                            if (placement[position - 9] == 4) {
                                if (placement[position - 18] == 0) {
                                    placement[position - 18] = 2;
                                }
                            }
                        }
                    }
                }
                if (placement[position] == 2) //MOVEMENT OF PIECE
                {
                    if ((placement[position - 14] == 5 || placement[position - 14] == 9) && (placement[position - 7] == 4 || placement[position - 7] == 8)&& placement[position] == 2) {
                        placement[position - 7] = 0;
                        whiteScore++;
                        redTurn = true;
                        turnTeller.setText("Red's Turn");
                    } else if ((placement[position - 18] == 5 || placement[position - 18] == 9) && (placement[position - 9] == 4 || placement[position - 7] == 8)&& placement[position] == 2) {
                        placement[position - 9] = 0;
                        whiteScore++;
                        redTurn = true;
                        turnTeller.setText("Red's Turn");
                    } else if (placement[position + 14] == 9 && (placement[position + 7] == 4  || placement[position + 7] == 8)&& placement[position] == 2) {
                        placement[position + 7] = 0;
                        whiteScore++;
                        redTurn = true;
                        turnTeller.setText("Red's Turn");
                    } else if (placement[position + 18] == 9 && (placement[position + 9] == 4 || placement[position + 9] == 8) && placement[position] == 2) {
                        placement[position + 9] = 0;
                        whiteScore++;
                        redTurn = true;
                        turnTeller.setText("Red's Turn");
                    }
                    for (int i = 0; i < placement.length; i++) {
                        if (placement[i] == 5) {
                            placement[position] = 3;
                            placement[i] = 0;
                            redTurn = true;
                            turnTeller.setText("Red's Turn");
                        } else if (placement[i] == 9) {
                            placement[position] = 7;
                            placement[i] = 0;
                            redTurn = true;
                            turnTeller.setText("Red's Turn");
                        }
                        if (placement[i] == 2) {
                            placement[i] = 0;
                        }
                    }
                }

                if (placement[1] == 56) //KING RED
                {
                    placement[1] = 7;
                } else if (placement[3] == 58) {
                    placement[3] = 7;
                } else if (placement[5] == 60) {
                    placement[5] = 7;
                } else if (placement[7] == 62) {
                    placement[7] = 7;
                }
                board.setAdapter(adapter);
            }
            if(!redTurn && ai)
            {

            }
        }
    });

}



public class ImageAdapter extends BaseAdapter {
    private Context context;
    int[] tiles = {R.drawable.blacktile, R.drawable.redtile, R.drawable.yellowtile,
                   R.drawable.whitepiece, R.drawable.redpiece, R.drawable.whitepieceselected, R.drawable.redpieceselected,
                   R.drawable.whiteking, R.drawable.redking, R.drawable.whitekingselected, R.drawable.redkingselected};
    public ImageAdapter (Context c) {context=c;}

    @Override
    public int getCount() {return placement.length;}

    @Override
    public Object getItem(int position)
    {
        return tiles[position];
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        pic = new ImageView(context);

        pic.setImageResource(tiles[placement[position]]);

            pic.setScaleType(ImageView.ScaleType.FIT_CENTER);
            pic.setLayoutParams(new GridView.LayoutParams(51,51));

            return pic;
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mcollins499.mcollins_prog11.MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <TextView
        android:id="@+id/Turn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="Red's Turn"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/board" />

    <GridView
        android:id="@+id/board"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="70dp"
        android:layout_marginEnd="42dp"
        android:layout_marginStart="42dp"
        android:layout_marginTop="105dp"
        android:clickable="false"
        android:focusable="false"
        android:contextClickable="false"
        android:numColumns="8"
        app:layout_constraintBottom_toTopOf="@+id/Turn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</android.support.constraint.ConstraintLayout>
0 = black tile  
1 = red tile  
2 = highlighted tile  
3 = black/white piece  
4 = red piece  
5 = highlighted black/white piece  
6 = highlighted red piece  
7 = black/white king  
8 = red king  
9 = highlighted black/white king  
10 = highlighted red king  
private boolean isPlaceEqual(int position, int offset, int value)
{
    int index = position + offset;
    if(index > -1 && index < placement.length)
    {
        return placement[index] == value;
    }

    return false;
}