2012-03-18 46 views
2

我有一個TabHost活動被交換的活動,所以我的主要如下所示:爲什麼我的TabHost子活動無法找到父級TabHost ID?

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:padding="5dp" > 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="-4dp" 
      android:layout_weight="0" /> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:padding="5dp" /> 
    </LinearLayout> 

</TabHost> 

而且我的孩子的活動XML的一個是一個ListView:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

    </ListView> 

</LinearLayout> 

而且在這個孩子的活動, (延伸ListActivity)我試圖尋父TabHost:

TabHost tabHost = (TabHost) getParent().findViewById(R.id.tabhost); 
tabHost.setCurrentTab(0); 

但是,該R.id.tabhost沒有找到(tabhost id cannot be resolved)。
我在做什麼錯?

+0

也許有超過1個父。你可以試着用[層次結構查看器](http://developer.android.com/guide/developing/tools/hierarchy-viewer.html)找到它,它也可以嵌入到eclipse中 – zapl 2012-03-18 19:00:07

回答

3

Make it android.R.id.tabhost 它會工作。

+0

是的!我只是在考慮XML ID聲明中的android前綴。而且的確如此,謝謝。 – 2012-03-18 19:07:08

+0

謝謝,對我來說非常完美。 – 2014-04-01 07:09:30