无法识别getSupportFragmentManager

时间:2015-01-27 16:33:38

标签: android android-fragments android-fragmentactivity

如下面的代码所示,MainActivity扩展了FragmentActivityTabsPagerAdapter扩展了FragmentPagerAdapter

我不知道为什么eclipse不能识别这一行

viewPager = new TabsPagerAdapter(getSupportFragmentManager(), this.fragList);

MainActivity

import java.util.List;
import com.example.settingsviewpagertabs.adapter.TabsPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
....
....
....
viewPager = new TabsPagerAdapter(getSupportFragmentManager(), this.fragList); //eclipse does not accept it

适配器

import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter {

List<Fragment> mFragList;

public TabsPagerAdapter(FragmentManager fm, List<Fragment> mFragList) {
    super(fm);
    // TODO Auto-generated constructor stub
    this.mFragList = mFragList;
}

@Override
public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    return this.mFragList.get(arg0);

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return this.mFragList.size();
}

}

Update_1

现在我使用以下导入,

当我使用

viewPager = new TabsPagerAdapter(getSupportFragmentManager(), this.fragList);

我收到以下消息:

Type mismatch: cannot convert from TabsPagerAdapter to ViewPager

我目前使用的导入是:

MainActivity

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;

适配器导入

import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

2 个答案:

答案 0 :(得分:3)

您没有导入正确的类(支持类)。变化:

import android.app.Fragment;
import android.app.FragmentTransaction;
在MainActivity中

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

答案 1 :(得分:1)

实际上这不是支持片段管理器的问题。这是fragment list的问题: 查看tabs适配器,导入import android.support.v4.app.Fragment;。但是,在FragmentActivity中,您导入import android.app.Fragment;。 - &GT;解决方案是更改FragmentActivity中的导入。