2013-09-16 59 views
1

這讓我非常瘋狂,我不知道發生了什麼。 我有一個可點擊的RelativeLayout內的TextView的XML佈局。TextView not found

<RelativeLayout 
      android:id="@+id/bg_section" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="10dp" 
      android:background="@color/almost_black" 
      android:clickable="true" 
      android:onClick="goToBG" 
      android:padding="10dp" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_centerVertical="true" 
       android:text="Go To B.G." 
       android:textColor="@color/white" 
       android:textSize="20sp" /> 

      <ImageView 
       android:id="@+id/bg_arrow" 
       android:layout_width="wrap_content" 
       android:layout_height="30dp" 
       android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" 
       android:layout_marginRight="-10dp" 
       android:src="@drawable/arrow_icon" /> 

      <TextView 
       android:id="@+id/current_bg_count" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_toLeftOf="@id/bg_arrow" 
       android:text="3" 
       android:textColor="@color/holo_blue" 
       android:textSize="22sp" /> 
     </RelativeLayout> 

現在在我的代碼,我嘗試更新的TextView「current_bg_count」

private void updateBGCount(){ 
    try{ 
     RelativeLayout bgSection = (RelativeLayout) findViewById(R.id.bg_section); 
     TextView bgCountTV = (TextView) bgSection.getChildAt(2); 
     bgCountTV.setText(tempBG.size()); 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
     Logger.d(TAG, "exception in updateBGCount"); 
    } 
} 

這使得在哪裏我的setText雖然RelativeLayout的是沒有發現問題就行了ResourceNotFountException。即使當我嘗試找到它只是像這樣的ID:

TextView bgCountTV = (TextView) findViewById(R.id.current_bg_count) 
bgCountTV.setText(tempBG.size()); 

它給出了同樣的錯誤。 佈局中的所有其他視圖都很容易找到並更新得很好。只有這一個TextView給我的問題。有誰知道問題是什麼?

+0

您是否嘗試過清理你的項目? 「項目 - >清潔...」 – codeMagic

+0

嘗試清理你的項目。 –

+1

tempBG是指什麼?這是一個數組? – wyoskibum

回答

4

您需要將您的ArrayListStringsize這行

setText(tempBG.size()) 

轉換由於tempBG.size()返回一個int然後setText()正在尋找一個資源與任何該收益的id。您正在使用this setText(ResId int) method,如果您有要用於設置文本的string resource,則使用此文件。

所以你使用eclipse將其更改爲

setText(String.valueOf(tempBG.size())); 
+0

它確實是這麼簡單。我無法相信。謝謝! – Jay

+1

必須是setText的問題,因爲這是使用getChild()和findViewById()嘗試都不起作用的唯一常見分母。 – wyoskibum

+0

就是這麼簡單,但不用擔心,經常發生;) – codeMagic

2

嘗試這樣

bgCountTV.setText(""+tempBG.size()); 

tempBG.size()設置字符串這應該工作

0

?檢查項目中是否存在錯誤和警告。有時在xml中有錯誤可讓您編譯,但無法正常工作。

0

問題是與此代碼

bgCountTV.setText(tempBG.size()); 

tempBG.size()怎麼把它具有相應的int該代碼將返回其被假定爲資源ID類似R.string.VARIABLE_NAME一個INT值它的TextView是假設爲ID值

你可以做什麼

bgCountTV.setText(tempBG.size()+""); 

OR

bgCountTV.setText(String.ValueOf(tempBG.size())); 

OR

bgCountTV.setText(tempBG.size().toString());