使用 onCreate() 的 TabActivity 监听器
我有一个有两个选项卡的项目。选项卡是在主类中创建的。这里我也添加了选项卡侦听器,以处理选项卡之间的更改。这是一个选项卡的即时信息:
TabHost tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Tab1.class);
spec = tabHost.newTabSpec("tab1").setIndicator("",
res.getDrawable(R.drawable.ic_tab_tab1))
.setContent(intent);
tabHost.addTab(spec);
侦听器方法:
public void onTabChanged(String tabName) {
if (tabName.equals("tab1")){
tab1.load();
}
}
第二个选项卡也类似。我的问题是,如果 onCreate() 方法只在 Tab1 和 Tab2 类中运行一次,我如何“强制”主类在选项卡更改后显示相应的 Activity?我收到 NullPointerException
选项卡的类是这样的:
public class Tab1 extends Activity{
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.tab1);
load();
}
public void load(){
//....
}
}
I have a project with two tabs. Tabs are created in the main class. Here I added the tablistener too, to handle the changes between the tabs. Here is one tab's instant:
TabHost tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Tab1.class);
spec = tabHost.newTabSpec("tab1").setIndicator("",
res.getDrawable(R.drawable.ic_tab_tab1))
.setContent(intent);
tabHost.addTab(spec);
The listener method:
public void onTabChanged(String tabName) {
if (tabName.equals("tab1")){
tab1.load();
}
}
And similar for the second tab too. My question is, if the onCreate() methods run only once in the Tab1 and Tab2 classes, how can I "force" the main class to show the corresponding Activity after the tab changes? I receive NullPointerException
The tabs' classes are something like this:
public class Tab1 extends Activity{
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.tab1);
load();
}
public void load(){
//....
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
onCreate()
方法仅在您的Activity
首次加载时调用一次。如果您想在每次查看
Activity
时执行任何功能,请将该功能放入onResume()
方法。onCreate()
method calls only once when yourActivity
first time loaded.If you want to perform any functionality on each time you view your
Activity
, put that functionality inonResume()
method.