FragmentPagerAdapter的构造函数未定义

时间:2015-01-26 14:44:40

标签: android android-fragments

我有Fragment托管viewPager,我在另一个套餐中创建了FragmentPagerAdapter。当我试图调用

时,在fragment。java文件中
adapter = new ViewPagerTabsAdapter(getFragmentManager(), fragList);

日食强调与红色波浪线相交并说

构造函数ViewPagerTabsAdapter(FragmentManager,List)未定义

我认为这是因为我使用的导入,但我不知道如何解决它,

请找到下面发布的导入代码:

frag3

import com.example.fragmentcommunication_00.adapter.ViewPagerTabsAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Toast;
....
....

private void initViews(View root) {
    // TODO Auto-generated method stub
    vPager = (ViewPager) root.findViewById(R.id.frag3_pager);       
    actionBar = getActivity().getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    adapter = new ViewPagerTabsAdapter(getFragmentManager(), fragList);
}

** ViewPagerTabsAdapter **:

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

public class ViewPagerTabsAdapter extends FragmentPagerAdapter {

List<Fragment> mFragList;

public ViewPagerTabsAdapter(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_2 : 导入如下,在frag3中,fragList.add未定义,getSupportFragmentManager()也未定义。在adapter其中一种方法中,它的签名如下:

@Override
public android.support.v4.app.Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

现在adapter中的导入是:

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

Frag3中的导入是:

   import java.util.ArrayList;
 import java.util.List;
import com.example.fragmentcommunication_00.adapter.ViewPagerTabsAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

2 个答案:

答案 0 :(得分:1)

您需要使用getSupportFragmentManager,因为您的构造函数会收到android.support.v4.app.FragmentManagergetFragmentManager会返回android.app.FragmentManager而不是

答案 1 :(得分:0)

在fag3中尝试替换
import android.app.Fragment; 同 import android.support.v4.app.Fragment;