这个怎么解决??总是要求更改构造函数 --> mAdapter = new TabsPagerAdapter(getFragmentManager());
Posted
技术标签:
【中文标题】这个怎么解决??总是要求更改构造函数 --> mAdapter = new TabsPagerAdapter(getFragmentManager());【英文标题】:How fix this ?? always asked to change constructor --> mAdapter = new TabsPagerAdapter(getFragmentManager()); 【发布时间】:2014-06-04 10:37:57 【问题描述】:我有 3 节课
主类 片段(寻呼机) 片段适配器但是,在片段中,有问题,任何人都可以帮助我吗??
public class AttractionFragment extends Fragment implements ActionBar.TabListener
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = "Solo", "Karanganyar", "Sukoharjo" ;
public AttractionFragment()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.fragment_attraction, container, false);
// Initilization
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
actionBar = getActivity().getActionBar();
//This my problem
mAdapter = new TabsPagerAdapter(getFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs)
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
@Override
public void onPageSelected(int position)
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
@Override
public void onPageScrollStateChanged(int arg0)
);
return rootView;
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
// TODO Auto-generated method stub
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
// TODO Auto-generated method stub
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
// TODO Auto-generated method stub
这是我的适配器类
public class TabsPagerAdapter extends FragmentStatePagerAdapter
public TabsPagerAdapter(FragmentManager fm)
super(fm);
@Override
public Fragment getItem(int index)
switch (index)
case 0:
// Top Rated fragment activity
return new SoloAttractionFragment();
case 1:
// Games fragment activity
return new KaranganyarAttractionFragment();
case 2:
// Movies fragment activity
return new SukoharjoAttractionFragment();
return null;
@Override
public int getCount()
// get item count - equal to number of tabs
return 3;
【问题讨论】:
仍然错误,我更改为 mAdapter = new TabsPagerAdapter(getSupportFragmentManager()); , [ AttractionFragment 类型的方法 getSupportFragmentManager() 未定义] 你需要使用getActivity().getSupportFragmentManager()
并且你的导入必须来自支持库
这一行有多个标记 - Activity 类型的方法 getSupportFragmentManager() 未定义 - 构造函数 TabsPagerAdapter(FragmentManager) 未定义
清单文件中的最小 sdk 是什么??
还是错误,我改成 mAdapter = new TabsPagerAdapter(getActivity().getSupportFragmentManager());
【参考方案1】:
您正在使用 FragmentStatePagerAdapter`。
http://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter.html
您可能还想检查
Does the Android ICS API have a native equivalent to ViewPager support lib?
你需要改变很多东西。
活动中
public class MainActivity extends ActionBarActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_view);
AttractionFragment newFragment = new AttractionFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
AttractionFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar.Tab;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AttractionFragment extends Fragment implements ActionBar.TabListener
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = "Solo", "Karanganyar", "Sukoharjo","Sragen","Boyolali","Klaten","Wonogiri" ;
public AttractionFragment()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.frag2, container, false);
// Initilization
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
//This my problem
mAdapter = new TabsPagerAdapter(getActivity().getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs)
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(AttractionFragment.this));
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()
@Override
public void onPageSelected(int position)
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
@Override
public void onPageScrolled(int arg0, float arg1, int arg2)
@Override
public void onPageScrollStateChanged(int arg0)
);
return rootView;
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
// TODO Auto-generated method stub
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
// TODO Auto-generated method stub
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
// TODO Auto-generated method stub
标签页适配器
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
public class TabsPagerAdapter extends FragmentStatePagerAdapter
public TabsPagerAdapter(android.support.v4.app.FragmentManager fragmentManager)
super(fragmentManager);
@Override
public Fragment getItem(int index)
Fragment fragment = null;
switch (index)
case 0:
// Top Rated fragment activity
fragment =new SoloAttractionFragment();
case 1:
fragment = new KaranganyarAttractionFragment();
case 2:
// Movies fragment activity
fragment = new SukoharjoAttractionFragment();
return fragment ;
@Override
public int getCount()
// get item count - equal to number of tabs
return 3;
捕捉
【讨论】:
还是报错,头晕,之前我是按照androidhive.info的教程做的,后来我修改了,因为我用的是带抽屉和滑动的导航标签。 @YeftaAndrea 你还想要什么。贴出代码。 psoted 快照。错误是什么。 拜托,我已经上传到Dropbox dropbox.com/s/vdxy6giip7r5p4a/yefta.rar @YeftaAndrea 你根本没有关注帖子,你说你仍然收到错误 我很抱歉,但非常感谢@Raghunandan 的帮助,我已经知道解决方案,我发现我必须删除导入片段,因为我必须使用片段 v4,但非常感谢: )【参考方案2】:请尝试更改
public TabsPagerAdapter(FragmentManager fm)
super(fm);
与
public TabsPagerAdapter(android.app.FragmentManager.FragmentManager fm)
super(fm);
尽量不要使用支持片段管理器。
不,它不会给出错误,因为我已经尝试过下面的示例代码。
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class MainActivity extends FragmentStatePagerAdapter
public MainActivity(FragmentManager fm)
super(fm);
@Override
public Fragment getItem(int arg0)
return null;
@Override
public int getCount()
return 0;
【讨论】:
如果我这样更改,在 super(fm) 行中将显示错误通知以像以前一样更改源代码。以上是关于这个怎么解决??总是要求更改构造函数 --> mAdapter = new TabsPagerAdapter(getFragmentManager());的主要内容,如果未能解决你的问题,请参考以下文章
Map::operator[] 总是需要一个默认的构造函数[重复]