FragmentPagerAdapter troubles and woes: Constructor Undefined
Solution 1
I have solved the problem. It was a myriad of solutions that was required.
First, I had to extend FragmentActivity.
public class Polling extends FragmentActivity {
Then, when creating my TabsAdapter, I had to send a call to this (the FragmentActivity itself):
mTabsAdapter = new TabsAdapter(this, mViewPager);
Then, when making the constructor in the TabsAdapter subclass, I had to change the parameter to FragmentActivity rather than Activity:
public TabsAdapter(FragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
Finally, I had to change my import statement in my Fragment classes to reflect the use of the compatibility library rather than the regular fragment package:
import android.support.v4.app.Fragment;
So, now my ViewPager functions. Of course, clicking on each individual tab icon to switch between tabs no longer works (so I am working on that solution as soon as I send this message). Regardless, now that the ViewPager functions, I can move on to more pressing issues in my app. Thanks to LouisLouis for starting me down the correct path. I eventually used CountingFragment.java to help me see where my errors were, as well as FragmentTabsPager.java from ActionBarSherlock's code to determine the need to specify FragmentActivity.
Solution 2
You need to change the lines
public TabsAdapter(Activity activity, ViewPager pager) {
super(activity.getFragmentManager());
to
public TabsAdapter(Activity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
This will supply the android.support.v4.app.FragmentManager
which is needed by the constructor for android.support.v4.app.FragmentPagerAdapter
. You are currently supplying android.app.FragmentManager
, which is what is returned from activity.getFragmentManager()
. That is causing a type mismatch which is the root cause of your error.
Davek804
Updated on August 22, 2022Comments
-
Davek804 over 1 year
Edit: I have reworked some of my code to try and get past the error (but now the app crashes on launch). First, my class now extends FragmentActivity so that I can access the method, getSupportFragmentManager();. Then, within on create, I changed my constructor:
mTabsAdapter = new TabsAdapter(this.getSupportFragmentManager(), this, mViewPager);
From there, in my subclass, I changed the code to reflect this:
public TabsAdapter(FragmentManager fm, Activity activity, ViewPager pager) { super(fm); mContext = activity; mActionBar = activity.getActionBar(); mViewPager = pager; mViewPager.setAdapter(this); mViewPager.setOnPageChangeListener(this); }
As I said, it still crashes, any ideas at this point?
Begin original question: I have the following error: “The constructor FragmentPagerAdapter(FragmentManager) is undefined”*
When I try to execute the following line:
super(activity.getFragmentManager());
this line follows mypublic TabsAdapter(Activity activity, ViewPager pager) {
which is extending FragmentPagerAdapter.Basically, I am trying to get my 8+ fragment app (8 fragment, one activity which hosts a set of ActionBar tabs linked to each fragment [only one is seen here for brevity's sake "loginTab"]).
Here’s my whole code:
Edit: Pages that I have used for reference: http://developer.android.com/reference/android/support/v4/view/ViewPager.html
http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/
package com.davekelley.polling; import java.util.ArrayList; import android.app.ActionBar; import android.app.ActionBar.Tab; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.widget.Button; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; public class Polling extends Activity { private ViewPager mViewPager; private TabsAdapter mTabsAdapter; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mViewPager = new ViewPager(this); mViewPager.setId(R.id.pager); setContentView(mViewPager); final ActionBar bar = getActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); bar.setDisplayShowTitleEnabled(false); bar.setDisplayShowHomeEnabled(false); mTabsAdapter = new TabsAdapter(this, mViewPager); mTabsAdapter.addTab(bar.newTab().setText(“Simple”), LoginFragment.class, null); mTabsAdapter.addTab(bar.newTab().setText(“Simple”), EconFragment.class, null); mTabsAdapter.addTab(bar.newTab().setText(“Simple”), PoliticsFragment.class, null); if (savedInstanceState != null) { bar.setSelectedNavigationItem(savedInstanceState.getInt(“tab”, 0)); } } protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(“tab”, getActionBar().getSelectedNavigationIndex()); } public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener { private final Context mContext; private final ActionBar mActionBar; private final ViewPager mViewPager; private final ArrayList mTabs = new ArrayList(); static final class TabInfo { private final Class clss; private final Bundle args; TabInfo(Class _class, Bundle _args) { clss = _class; args = _args; } } public TabsAdapter(Activity activity, ViewPager pager) { super(activity.getFragmentManager()); mContext = activity; mActionBar = activity.getActionBar(); mViewPager = pager; mViewPager.setAdapter(this); mViewPager.setOnPageChangeListener(this); } public void addTab(ActionBar.Tab tab, Class clss, Bundle args) { TabInfo info = new TabInfo(clss, args); tab.setTag(info); tab.setTabListener(this); mTabs.add(info); mActionBar.addTab(tab); notifyDataSetChanged(); } public int getCount() { return mTabs.size(); } public Fragment getItem(int position) { TabInfo info = mTabs.get(position); return Fragment.instantiate(mContext, info.clss.getName(), info.args); } public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } public void onPageSelected(int position) { mActionBar.setSelectedNavigationItem(position); } public void onPageScrollStateChanged(int state) { } public void onTabSelected(Tab tab, FragmentTransaction ft) { Object tag = tab.getTag(); for (int i=0; i<mTabs.size(); i++) { if (mTabs.get(i) == tag) { mViewPager.setCurrentItem(i); } } } public void onTabUnselected(Tab tab, FragmentTransaction ft) { } public void onTabReselected(Tab tab, FragmentTransaction ft) { } public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) { } public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) { } public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) { } } }
Any idea what's going on here and what I can do to fix it? My Fragment classes are very simple at the moment, but will eventually load data from an SQL server (at the meantime, they just return a simple inflated layout).
-
Davek804 about 12 yearsI have edited my code to read as: public TabsAdapter(Activity activity, ViewPager pager) { super(activity.getSupportFragmentManager()); and now receive the following error: The method getSupportFragmentManager() is undefined for the type Activity. Eclipse offers to cast Activity or change the method back to what it was, getFragmentManager(). Any ideas?
-
Sam Dozor about 12 yearsUse a android.support.v4.app.FragmentActivity instead of regular Activity
-
louielouie about 12 yearsGlad to hear it is working now. It can be tricky when first starting to use the compatibility library.
-
Davek804 about 12 yearsSure Will, I'll do my best to transform this post + one comment into a post over on Meta - but what aspect do you think is the bug?
-
Davek804 about 12 yearsPosted as a bug: meta.stackexchange.com/questions/126700/…
-
cammando about 7 yearsNow use AppCompatAcitivity and will take care of the rest thing. or you may extend fragment in order to use that here.