使用 onCreate() 的 TabActivity 监听器

发布于 2024-12-29 16:55:30 字数 1012 浏览 5 评论 0原文

我有一个有两个选项卡的项目。选项卡是在主类中创建的。这里我也添加了选项卡侦听器,以处理选项卡之间的更改。这是一个选项卡的即时信息:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少心凉 2025-01-05 16:55:30

onCreate() 方法仅在您的 Activity 首次加载时调用一次。

如果您想在每次查看 Activity 时执行任何功能,请将该功能放入 onResume() 方法。

onCreate() method calls only once when your Activity first time loaded.

If you want to perform any functionality on each time you view your Activity, put that functionality in onResume() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文