java.lang.IllegalStateException:Action Bar Tab必须有一个Callback

时间:2014-12-10 12:43:15

标签: android

我正在开发一个应用程序,我想在其中使用android操作栏选项卡。我使用了操作栏标签。但是当我运行应用程序时,它给了我一个例外。它给出异常" java.lang.IllegalStateException:Action Bar Tab必须有一个Callback"。请帮助我,并提前致谢

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setDisplayShowTitleEnabled(false);
        Tab tab = actionBar.newTab().setText("xyzabc");
        actionBar.addTab(tab);
        tab.setTabListener(new TabListener() {

            @Override
            public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }
        });

        Tab tab1 = actionBar.newTab().setText("pqrabc");
        actionBar.addTab(tab1);
        tab1.setTabListener(new TabListener() {

            @Override
            public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
                // TODO Auto-generated method stub

            }
        });
}
}

1 个答案:

答案 0 :(得分:0)

首先应设置标签监听器,然后将其添加到操作栏:

    Tab tab1 = actionBar.newTab().setText("pqrabc");
    tab1.setTabListener(new TabListener() {...})
    actionBar.addTab(tab1);