簡體   English   中英

在Android中遇到findViewById的問題

[英]Having trouble with findViewById in Android

我有一個for循環,它通過與我的'Board'類中的imageViews對應的imageView ID數組運行。 代碼如下:

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));   

我在findViewById和possibleSquare時遇到錯誤,特別是Android“無法解析”。

這是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"
            />

並且還有61個以上類似的imageView。

我對於findViewById遇到的確切錯誤是:'無法解析方法findViewById(?)

possibleSquare變量的錯誤是“無法解析符號possibleSquare”。

我究竟做錯了什么?

我認為您誤解了對象在Java中的工作方式。 possibleSquare語義上來說, R.id.possibleSquareR.id.possibleSquare完全不同。 我假設您提供的視圖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));   
}

請注意,如果您只是想遍歷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));   
}

您不能那樣做,因為R.id.possibleSquare本質上是一個變量名。 它不是可用於替換內容的字符串

您可能想看看這種方法

int resID = context.getResources().getIdentifier(possibleSquare, "id", PACKAGENAME);

它將嘗試將字符串與資源ID匹配。 我用它來收縮可繪制對象的ResID。 從未使用過它作為ID,但可能對您有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM