2013-07-13 66 views
0

我只是試圖更改我的選項卡名稱,因爲我在我的選項卡中包含了佈局,並且我無法更改XML中的名稱或至少找不到方法。Android:更改選項卡名稱 - nullPointerException

這是佈局(部分):

<FrameLayout 
android:id="@android:id/tabcontent" 
android:layout_width="match_parent" 
android:layout_height="360dp" 
android:layout_above="@+id/button_gamecfg_save" > 

<include 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
layout="@layout/gamecfg_times" /> 

<include 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
layout="@layout/gamecfg_others" /> 

<include 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
layout="@layout/gamecfg_roles" /> 

</FrameLayout> 

而且我的代碼:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gamecfg_tabs); 
    TabHost th=((TabHost)findViewById(android.R.id.tabhost)); 
    ((TextView)th.getTabWidget().getChildAt(0).findViewById(android.R.id.title)).setText("New"); //<-------------------- 
} 

在我的代碼必須在標線一個NullPointerException。我可以輕鬆獲得tabwidget,但只要我嘗試getChildAt,我有例外,但我不明白爲什麼。

你能幫我嗎?

+0

嘗試'setIndicator(「Data」)'而不是'setText(「New」)' –

+0

放置斷點並檢查你得到的「th」。 –

+0

我得到了GetChildAt的錯誤,這是因爲tabwidget沒有孩子,但它不可能嗎?我添加了3個佈局,SDK向我顯示了3個選項卡!這是怎麼發生的? – rXp

回答

0

您是否嘗試在getChildAt(0)之前調用方法th.setup()?

該文檔說: 「如果使用findViewById()加載TabHost,則在添加選項卡之前調用setup()」。

相關問題