如何从另一个活动调用 TabActivity getTabHost()?避免 NullPointerException
我正在学习android。 想要实现标签式浏览功能;为此,我有以下活动
public class CustomList extends ListActivity {
....
and inside this (and other activities I want to put the tabs)
for example:
Resources res = getResources();
**TabActivity ta = new TabActivity();**
TabHost host = **ta.getTabHost();** // this line returns **NullPointerException**
host.addTab(host.newTabSpec("first")
.setIndicator("First")
.setContent(new Intent(this, First.class)));
These lines are included inside OnCreate().
可能最好有带有选项卡定义的单独活动,并在所有其他活动中调用它。但是,我不知道该怎么做。
请提出解决此问题的建议。先感谢您。
Im in a process of learning android.
Want to implement tabbed browsing functionality; For this purpose I have following activity
public class CustomList extends ListActivity {
....
and inside this (and other activities I want to put the tabs)
for example:
Resources res = getResources();
**TabActivity ta = new TabActivity();**
TabHost host = **ta.getTabHost();** // this line returns **NullPointerException**
host.addTab(host.newTabSpec("first")
.setIndicator("First")
.setContent(new Intent(this, First.class)));
These lines are included inside OnCreate().
Probably it would be better to have separate activity with tab definitions and just call it in every other activities. But, Im not sure how to do this.
Please for suggestion to solve this issue. Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您想要的是在一个选项卡中拥有多个活动。
为此,您必须首先创建一个扩展 TabActivity 的 Activity,并使用扩展 TabGroupActivity 的活动初始化其选项卡。
以下链接提供了有关如何在一个中进行多个活动的更多信息选项卡活动
I think what you want is to have multiple activity within a single tab.
In order to do this, you have to first create an Activity which extend TabActivity, and initialize its tabs with activities extending TabGroupActivity.
Here is a link with more informations on how to have multiple activities in a TabActivity