Android 2.1 中的 TabHost 和 AsyncTasks
我的应用程序启动时带有一个具有四个选项卡的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论