Android 2.1 中的 TabHost 和 AsyncTasks

发布于 2024-11-02 16:26:31 字数 2095 浏览 4 评论 0原文

我的应用程序启动时带有一个具有四个选项卡的 TabActivity,每个选项卡都有一个关联的 ListView 活动作为其内容。理想情况下,我想在 onCreate 中启动一个 AsyncTask 来初始化 Drupal XMLRPC 连接,完成后,我创建 tabHost 并在 onPostExecute 中添加选项卡。这在 Android 2.2 及更高版本中工作正常,但在 2.2 及更低版本中会导致立即强制关闭。据我所知,似乎在 Android 2.2 较低版本中,TabActivity 要求在完成其他操作(包括 AsyncTasks)之前创建选项卡?如果有人对如何实现在 setCurrentTab 中启动活动之前运行 AsyncTask 的 TabActivity 有任何建议,我将不胜感激。这是我正在使用的 onCreate 初始化 tabHost 以供参考:

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    tabHost = getTabHost(); //The activity TabHost
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
    TabHost.TabSpec spec; //Reusable TabSpec for each tab
    Intent intent; //Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, BlogList.class);
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("Blog Posts").setIndicator("Blog Posts")
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, DiariesList.class);
    spec = tabHost.newTabSpec("Diaries").setIndicator("Diaries")
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, BoardList.class);
    spec = tabHost.newTabSpec("MGoBoard").setIndicator("MGoBoard")
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, UserActivityList.class);
    spec = tabHost.newTabSpec("My Account").setIndicator("My Account")
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 50;
    tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 50;
    tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = 50;
    tabHost.getTabWidget().getChildAt(3).getLayoutParams().height = 50;
    tabHost.setCurrentTab(0);
}   

My app launches with a TabActivity that has four tabs, each with an associated ListView activity as its content. Ideally, I would like to launch an AsyncTask in onCreate that initializes a Drupal XMLRPC connection, and after it is finished, I create the tabHost and add the tabs in the onPostExecute. This works fine in Android 2.2 and higher, but will cause an immediate force close in 2.2 and lower. From what I've read, it seems that in Android 2.2 lower, TabActivity requires that the tabs be created before anything else is done, including AsyncTasks? If anyone has any suggestions on how to implement a TabActivity that runs an AsyncTask before starting the activity in setCurrentTab, I would be most grateful. This is the onCreate I'm using that initializes the tabHost for reference:

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    tabHost = getTabHost(); //The activity TabHost
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
    TabHost.TabSpec spec; //Reusable TabSpec for each tab
    Intent intent; //Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, BlogList.class);
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("Blog Posts").setIndicator("Blog Posts")
                  .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, DiariesList.class);
    spec = tabHost.newTabSpec("Diaries").setIndicator("Diaries")
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, BoardList.class);
    spec = tabHost.newTabSpec("MGoBoard").setIndicator("MGoBoard")
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, UserActivityList.class);
    spec = tabHost.newTabSpec("My Account").setIndicator("My Account")
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 50;
    tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 50;
    tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = 50;
    tabHost.getTabWidget().getChildAt(3).getLayoutParams().height = 50;
    tabHost.setCurrentTab(0);
}   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文