【问题标题】:Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?您是否忘记调用“public void setup(LocalActivityManager activityGroup)”?
【发布时间】:2014-04-25 10:15:44
【问题描述】:

我正在使用片段选项卡编写一个简单的选项卡视图(因为不推荐使用 TabActivity)

但是当我尝试运行它时,它会提示错误“您是否忘记调用 'public void setup(LocalActivityManager activityGroup)'?”。另外我注意到 ActivityGroup 和 setup(void) 也被弃用了。

如何解决?

public class ListTab extends FragmentActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_tab);

        FragmentTabHost listTab = (FragmentTabHost)findViewById(R.id.list_tab_host);

        listTab.setup(this, this.getSupportFragmentManager(), R.id.realtabcontent);

        TabSpec allPostSpec = listTab.newTabSpec("all_post");
        allPostSpec.setIndicator("All");
        Intent allPostIntent = new Intent(this,ListPost.class);
        allPostSpec.setContent(allPostIntent);

        listTab.addTab(allPostSpec);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.list_post, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

  • 如果您正在使用 FragmentTabHost,请不要将 TabSpec 与 Intent 一起使用...使用这个 addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) 版本的 addTab(请记住,clss 应该是扩展 Fragment 的类)
  • 我把它改成:listTab.addTab(listTab.newTabSpec("all_post").setIndicator("All"), ListPost.class, null);其中 ListPost 是扩展 ListFragment 但错误仍然相同:(

标签: android tabs fragment


【解决方案1】:

尝试使用带有 addTab(TabSpec, FragmentClass, Bundle) 的 FragmentTabHost 类

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.fragment_tabs);
     mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
     mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
     mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
     FragmentStackSupport.CountingFragment.class, null);
     mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
                     LoaderCursorSupport.CursorLoaderListFragment.class, null);
     mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                     LoaderCustomSupport.AppListFragment.class, null);
     mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
                     LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
}

其中 R.id.realtabcontent 是您的容器。

你可以在这里找到一个例子:

http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html

【讨论】:

  • 请将链接的相关部分粘贴到答案中。如果链接发生变化,那么未来的人可能无法找到它。
  • 好的好的,已修复,抱歉,第一次发帖。
猜你喜欢
  • 2011-03-17
  • 2014-06-30
  • 2022-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-27
相关资源
最近更新 更多