1

Good day guys. I'm trying to build a simple app which support tab layout with a swipe view feature. I follow this tutorial but no success.

package com.example.project.project;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.app.ActionBar;
import android.view.Menu;

public class TabMain extends FragmentActivity implements ActionBar.TabListener{

    private ViewPager viewPager;
    private ActionBar actionBar;
    private TabsFragmentPagerAdapter tabsAdapter;
    private String[] days = new String[]{"Information","Claim"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_main);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabsAdapter = new TabsFragmentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(tabsAdapter);
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        for(int i=0; i<2; i++){
            actionBar.addTab(actionBar.newTab().setText(days[i]).setTabListener(this));
        }
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg) {
                // TODO Auto-generated method stub
                actionBar.setSelectedNavigationItem(arg);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

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

    }


    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
        viewPager.setCurrentItem(tab.getPosition());
    }


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

    }
}

I have follow each single step from the tutorial, but I get error as below.

Error:(13, 8) error: TabMain is not abstract and does not override abstract method onTabReselected(Tab,FragmentTransaction) in TabListener
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

And I get a red line underneath this statement public class TabMain extends FragmentActivity implements ActionBar.TabListener{

bsiamionau
  • 8,099
  • 4
  • 46
  • 73
John
  • 684
  • 11
  • 35

1 Answers1

1

You're using the wrong FragmentTransaction class for that interface, which uses android.app.FragmentTransaction, not android.support.v4.app.FragmentTransaction. Since the your method signature doesn't match the interface's, you're not actually implementing the correct method. The old ActionBar and it's related classes don't work with those support classes.

ActionBar.TabListener docs

Please note that that class had been deprecated, and Google recommends that you change how you handle navigation within your app.

Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • 1
    Nah, it's not a stupid mistake. It's quite easy to get those mixed up, since they kept the same class names in the support package. Things got a little confusing when they released that library. – Mike M. Nov 20 '15 at 04:46
  • @John I neglected to mention that, if you do want to keep ActionBar tabs, the v7 support package has a [replacement for that interface](http://developer.android.com/reference/android/support/v7/app/ActionBar.TabListener.html). – Mike M. Nov 21 '15 at 12:56