1

我的应用启动时有一个 TabActivity,它有四个选项卡,每个选项卡都有一个关联的 ListView 活动作为其内容。理想情况下,我想在 onCreate 中启动一个初始化 Drupal XMLRPC 连接的 AsyncTask,完成后,我创建 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);
}   
4

0 回答 0