0

我只是想更改我的选项卡名称,因为我在选项卡中包含了布局,我无法更改 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,但是一旦我尝试 getChildAt,我就会遇到异常,但我不明白为什么。

你能帮助我吗 ?

4

1 回答 1

0

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

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

于 2013-07-14T14:15:08.707 回答