在Android中的非预期输出

huangapple go评论60阅读模式
英文:

Not Expected Output in Android

问题

这是我在所有网格元素的 XML 文件中添加的 onTap 函数:

public void onTap(View view) {
    final ImageView[] box = new ImageView[9];
    for (int i = 1; i < 10; i++) {
        box[i] = findViewById(R.id.imageView + i);
        final int k = i;
        box[i].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                box[k].setImageResource(R.drawable.o);
            }
        });
    }
}
英文:

I am trying to build a simple tic tac toe game in Android Studio and want to display the O's and X's whenever the user clicks on the grid element but it isn't showing the expected output and whenever i tried to click on the grid in the tic tac toe then it shows the app not responding error.

This is my onTap func which i have added in all the grid elements in the xml file:

public void onTap(View view) {
     final ImageView[] box = new ImageView[9];
     for (int i = 1; i &lt; 10; i++) {
          box[i] = findViewById(R.id.imageView + i);
         final int k = i;
         box[i].setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 box[k].setImageResource(R.drawable.o);
             }
         });
    }

 }

答案1

得分: 1

以下是翻译好的部分:

有几个问题在这里。首先:

final ImageView[] box = new ImageView[9];
for (int i = 1; i < 10; i++) {

在Java中,数组索引从0开始,一个有9个项目的数组的索引范围是从0到8。将循环改为:

for (int i = 0; i < 9; i++) {

接下来的问题在这里:

box[i] = findViewById(R.id.imageView + i);

视图ID不是连续的数字。你不能只是将 i 添加到现有的视图ID中,并希望结果是一个有效的ID。

假设你的视图ID 是 R.id.imageView1-R.id.imageView9,你可以将创建 box 数组的代码更改为:

ImageView[] box = {
    findViewById(R.id.imageView1), 
    findViewById(R.id.imageView2),
    ...
    findViewById(R.id.imageView9)
}
英文:

There are a few things wrong here. First:

 final ImageView[] box = new ImageView[9];
 for (int i = 1; i &lt; 10; i++) {

In Java array indices start with 0, and an array with 9 item has indices 0 through 8. Change the loop to:

 for (int i = 0; i &lt; 9; i++) {

The next problem is here:

 box[i] = findViewById(R.id.imageView + i);

View IDs are not consecutive numbers. You cannot just add i to an existing view ID and hope that the result a valid ID.

Assuming your views have IDs R.id.imageView1-R.id.imageView9, you could change the code that creates the box array to:

 ImageView[] box = {
     findViewById(R.id.imageView1), 
     findViewById(R.id.imageView2),
     ...
     findViewById(R.id.imageView9)
 }

答案2

得分: 0

你可以为你的ID声明一个String,并像这样使用findViewById()

for (int i = 0; i < 9; i++) {
   String boxID = "imageView" + i;
   int resID = getResources().getIdentifier(boxID, "id", getPackageName());
   box[i] = findViewById(resID);
   box[i].setOnClickListener(this);
}
英文:

You can declare a String for your ID and use the findViewById() like this

for (int i = 0; i &lt; 9; i++) {
   String boxID = &quot;imageView&quot;+ i;
   int resID = getResources().getIdentifier(boxID, &quot;id&quot;, getPackageName());
   box[i] = findViewById(resID);
   box[i].setOnClickListener(this);
}

huangapple
  • 本文由 发表于 2020年7月26日 19:37:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63099614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定