Android 2.1中的TabHost和AsyncTasks

时间:2011-04-20 01:28:41

标签: android android-asynctask android-tabhost tabactivity

我的应用程序使用TabActivity启动,该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);
}   

0 个答案:

没有答案