2013-07-13 64 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()”。

相关问题