Android:更改选项卡名称 - nullPointerException

时间:2013-07-13 21:00:46

标签: android tabs

我只是想改变我的标签名称,因为我在选项卡中包含了布局,我无法更改XML中的名称,或者至少我找不到如何。

这是布局(部分):          

<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但是当我尝试getChild时我有例外但我不明白为什么。

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

您是否尝试在getChildAt(0)之前调用方法th.setup()?

文件说: “如果使用findViewById()加载TabHost,则在添加选项卡之前调用setup()。”