[英]Having trouble with findViewById in Android
I have a for loop which is running through an an array of imageView ID's that correspond to imageViews in my 'Board' class. 我有一个for循环,它通过与我的'Board'类中的imageViews对应的imageView ID数组运行。 The code is below: 代码如下:
for (String s : chunks) {
String possibleSquare = "s" + s.substring(2, 4);
ImageView backgroundImg = (ImageView) findViewById(R.id.possibleSquare);
backgroundImg.setBackgroundColor(Color.rgb(255, 255, 255));
I'm getting an error with findViewById and possibleSquare, specifically that Android 'cannot resolve' either. 我在findViewById和possibleSquare时遇到错误,特别是Android“无法解析”。
Here is the xml: 这是xml:
<TextView
android:id="@+id/BoardHeading"
android:text="Chessboard"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ff2d3017"
android:textSize="20dp"
android:textAlignment="center" />
<TableLayout
android:layout_width="360dp"
android:layout_height="320dp"
android:layout_below="@id/BoardHeading"
android:gravity="center_horizontal"
android:background="#FFFFFF" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/s00"
android:tag="black_rook"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#F3E5AB"
android:src="@drawable/black_rook"
/>
<ImageView
android:id="@+id/s01"
android:tag="black_knight"
android:layout_width="0dp"
android:layout_height="40dp"
android:background="#AF9B60"
android:layout_weight="1"
android:src="@drawable/black_knight"
/>
<ImageView
android:id="@+id/s02"
android:tag="black_bishop"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#F3E5AB"
android:src="@drawable/black_bishop"
/>
And there are 61 more imageViews similar to the ones above. 并且还有61个以上类似的imageView。
The exact error I'm getting for the findViewById is: 'Cannot resolve method findViewById(?) 我对于findViewById遇到的确切错误是:'无法解析方法findViewById(?)
The error for the possibleSquare variable is 'Cannot resolve symbol possibleSquare'. possibleSquare变量的错误是“无法解析符号possibleSquare”。
What am I doing wrong? 我究竟做错了什么?
I think you're misunderstanding how objects work in Java. 我认为您误解了对象在Java中的工作方式。 possibleSquare
is a very different thing semantically than R.id.possibleSquare
. possibleSquare
语义上来说, R.id.possibleSquare
与R.id.possibleSquare
完全不同。 I'm assuming you've given your views IDs that look like android:id="@+id/sSOMETHING"
我假设您提供的视图ID类似于android:id="@+id/sSOMETHING"
for (String s : chunks) {
String possibleSquare = "s" + s.substring(2, 4);
int id = getResources().getIdentifier(possibleSquare, "id", getPackageName());
ImageView backgroundImg = (ImageView) findViewById(id);
backgroundImg.setBackgroundColor(Color.rgb(255, 255, 255));
}
Note that if you're just looking to iterate over the TableRow
, you could also do something like this: 请注意,如果您只是想遍历TableRow
,则还可以执行以下操作:
TableRow tableRow = (TableRow)findViewById(R.id.tableRow1);
int childCount = tableRow.getChildCount();
for (int i = 0; i < childCount; i++){
ImageView backgroundImg = (ImageView) tableRow.getChildAt(i);
backgroundImg.setBackgroundColor(Color.rgb(255, 255, 255));
}
You can't do it that way as R.id.possibleSquare is essentially a variable name. 您不能那样做,因为R.id.possibleSquare本质上是一个变量名。 It is not a string that you can use to replace content 它不是可用于替换内容的字符串
You may want to look at this method 您可能想看看这种方法
int resID = context.getResources().getIdentifier(possibleSquare, "id", PACKAGENAME);
It will try to match the string to a resource ID. 它将尝试将字符串与资源ID匹配。 I used it to constract the ResID of a drawable. 我用它来收缩可绘制对象的ResID。 Never used it for ID but it may work for you 从未使用过它作为ID,但可能对您有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.