1

我刚刚将 v7 支持库添加到我的项目中,现在出现以下错误:

Description Resource    Path    Location    Type
The method setTabListener(ActionBar.TabListener) in the type ActionBar.Tab is not applicable for the arguments (ActionBar.TabListener)  TabsViewPagerFragmentActivity.java  /LoginAndRegistration/src/com/example/loginandregistration  line 76 Java Problem
 The method setTabListener(ActionBar.TabListener) in the type ActionBar.Tab is not applicable for the arguments (ActionBar.TabListener) TabsViewPagerFragmentActivity.java  /LoginAndRegistration/src/com/example/loginandregistration  line 80 Java Problem
The type new ActionBar.TabListener(){} must implement the inherited abstract method ActionBar.TabListener.onTabUnselected(ActionBar.Tab, FragmentTransaction)   TabsViewPagerFragmentActivity.java  /LoginAndRegistration/src/com/example/loginandregistration  line 52 Java Problem
The type new ActionBar.TabListener(){} must implement the inherited abstract method ActionBar.TabListener.onTabReselected(ActionBar.Tab, FragmentTransaction)   TabsViewPagerFragmentActivity.java  /LoginAndRegistration/src/com/example/loginandregistration  line 52 Java Problem
The type new ActionBar.TabListener(){} must implement the inherited abstract method ActionBar.TabListener.onTabSelected(ActionBar.Tab, FragmentTransaction) TabsViewPagerFragmentActivity.java  /LoginAndRegistration/src/com/example/loginandregistration  line 52 Java Problem
The method setTabListener(ActionBar.TabListener) in the type ActionBar.Tab is not applicable for the arguments (ActionBar.TabListener)  TabsViewPagerFragmentActivity.java  /LoginAndRegistration/src/com/example/loginandregistration  line 72 Java Problem

这是我的 TabsViewPagerFragmentActivity.java:

package com.example.loginandregistration;

import android.app.ActionBar.Tab;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.LocalBroadcastManager;
import com.example.loginandregistration.MyPageAdapter;
import android.support.v7.app.ActionBar;

class TabsViewPagerFragmentActivity extends FragmentActivity {

// Declare Variables
android.app.ActionBar ActionBar;
ViewPager mPager;
Tab tab;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from dashboard.xml
    setContentView(R.layout.dashboard);

    // Activate Navigation Mode Tabs
    ActionBar = getActionBar();
    ActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Locate ViewPager in dashboard.xml
    mPager = (ViewPager) findViewById(R.id.pager);

    // Capture ViewPager page swipes
    ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            // Find the ViewPager Position
            ActionBar.setSelectedNavigationItem(position);
        } 
    };

    mPager.setOnPageChangeListener(ViewPagerListener);
    // Locate the adapter class called ViewPagerAdapter.java
    MyPageAdapter viewpageradapter = new MyPageAdapter(getSupportFragmentManager());
    // Set the View Pager Adapter into ViewPager
    mPager.setAdapter(viewpageradapter);

    // Capture tab button clicks
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

         public void onTabSelected(Tab tab, FragmentTransaction ft) {
             // Pass the position on tab click to ViewPager
             mPager.setCurrentItem(tab.getPosition());
         }

         public void onTabUnselected(Tab tab, FragmentTransaction ft) {
             // TODO Auto-generated method stub
         }

         public void onTabReselected(Tab tab, FragmentTransaction ft) {
             // TODO Auto-generated method stub
         }

     };



    // Create first Tab
    tab = ActionBar.newTab().setText("Tab1").setTabListener(tabListener);
    ActionBar.addTab(tab);

    // Create second Tab
    tab = ActionBar.newTab().setText("Tab2").setTabListener(tabListener);
    ActionBar.addTab(tab);

    // Create third Tab
    tab = ActionBar.newTab().setText("Tab3").setTabListener(tabListener);
    ActionBar.addTab(tab);

}

}

我不知道如何修复 setTabListener 错误,但是关于错误:

The type new ActionBar.TabListener() ect.

当我添加未实现的方法时,我注意到它们与我的 onTabSelected、onTabUnselected、onTabReselected 方法相同。

我怎样才能解决这个问题?

4

2 回答 2

2

替换进口

import android.app.ActionBar.Tab;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

import android.support.v7.app.ActionBar.Tab;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

支持库有一个单独的设置类。

编辑

你还需要更换

android.app.ActionBar ActionBar;

ActionBar ActionBar;

使用支持库操作栏。此外,也许最好actionBar为变量名使用小写字母以使其与类名不同。

于 2013-09-28T23:09:13.770 回答
0

确保您正在实施 ActionBar.TabListener。this 引用了该实现,而不是片段/活动本身。

于 2015-03-30T13:50:01.560 回答