TabMain不是抽象的,并且不会覆盖TabListener

时间:2015-11-20 04:19:11

标签: java android android-fragments tabs swipeview

大家好。我正在尝试使用滑动视图功能构建一个支持标签布局的简单应用。我遵循这个tutorial但没有成功。

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

    }
}

我已经按照教程中的每一步操作,但是我收到如下错误。

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.

我在这句话public class TabMain extends FragmentActivity implements ActionBar.TabListener{

下面有一条红线

1 个答案:

答案 0 :(得分:1)

您使用了错误的FragmentTransaction类,该接口使用android.app.FragmentTransaction,而不是android.support.v4.app.FragmentTransaction。由于您的方法签名与接口不匹配,因此您实际上并未实现正确的方法。旧ActionBar及其相关类不适用于这些支持类。

ActionBar.TabListener docs

请注意,该课程已被弃用,Google建议您更改应用中导航的处理方式。