The method getChildFragmentManager() is undefined - android

I have a SherlockFragmentActivity, this Activity have a SectionsPagerAdapter and a ViewPager. When I call the constructor of the SectionsPagerAdapter with getChildFragmentManager() method, it shows me this error message:
The method getChildFragmentManager() is undefined for the type ViewPagerActivity
This is my code:
public class ViewPagerActivity extends SherlockFragmentActivity implements OnPageChangeListener, OnQueryTextListener{
private SectionsPagerAdapter sectionsPagerAdapter;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_view_pager);
sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager(), this);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.setOnPageChangeListener(this);
this.getMovements();
this.getPrizes();
this.getVouchers();
this.createDropdownlist();
}
I'm using ActionBarSherlock and I updated the android-support-v4.jar using "Android Tools - Add Support Library" in my project, also on ActionBarSherlock project.
I'm doing this because I need to have a ListFragment inside a Fragment that I use like a page on my ViewPager.

getChildFragmentManager() is a method on Fragment, not on FragmentActivity. Use getSupportFragmentManager() on a FragmentActivity.

Related

fragment in view pager not working properly

I implemented view pager in my activity like :
class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "FIRST", "SECOND", "THIRD" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
getActionBar().setHomeButtonEnabled(false);
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
getActionBar().addTab(getActionBar().newTab().setText(tab_name)
.setTabListener(this));
}
but when I swipe between tabs it never call the oncreateview of THIRD fragment but it actually call the oncreateview of THIRD fragment when I swipe to the SECOND fragment (From first to Second) and the oncreate view of second fragment get call along with the first fragment.
I don't understand what's wrong with the flow.
There is nothing wrong with this. when you swipe to new page in viewpager, it automatically loads the two neighbor fragments of current visible fragment. why? because of having better UX.
Can I prevent it from doing that?
No.
Can I extend it to load more neighbor fragments?
yes, setOffscreenPageLimit

FragmentManager is always null

so this one really has me stumped. I am trying to create a simple ViewPager activity which has three swipe views containing a listview. I have a FragmentPagerAdapter, a ListFragment set to hold the listviews, and have defined the layout for the fragments. Here is my problem is that FragmentPagerAdapter takes a FragmentManager, so I try to pass it in like so
mFragmentPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager());
but the FragmentManager is always null, meaning that the rest of the work to set up the pager crashes the app
I have no idea why getSupportFragmentManager would return null.
Here is the code for my activity, or at least the relevant bits
public class JobListActivity extends FragmentActivity {
private ArrayList<Job> jobsQuote=new ArrayList<Job>();
private ArrayList<Job> jobsIn= new ArrayList<Job>();
private ArrayList<Job> jobsOut=new ArrayList<Job>();
private ListFragment quoteListFragment=new ListFragment();
private ListFragment inListFragment =new ListFragment();
private ListFragment outListFragment=new ListFragment();
JobFragmentPagerAdapter mFragmentPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_list);
FragmentManager fm = getSupportFragmentManager();
mFragmentPagerAdapter =
new JobFragmentPagerAdapter(fm);
mViewPager = (ViewPager) findViewById(R.id.jobs_view_pager);
mViewPager.setAdapter(mFragmentPagerAdapter);
I really appreciate any help you can provide, thanks so much for your time
Here is the tutorial I have been following from the android developer web site
http://developer.android.com/training/implementing-navigation/lateral.html
clearly I am missing something.
Figured it out. When I implement the methods from Fragment activity, I accidentily overrode getSupportFragmentManager(); to return null.

FragmentPagerAdapter and notifyDataSetChanged

How can I use notifyDataSetChanged with FragmentPagerAdapter ?
In onResume:
I used mMyFragmentPagerAdapter.notifyDataSetChanged(); but it didnt worked
But mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager()); is worked
public class Activity extends FragmentActivity{
static ViewPager mViewPager;
private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = (ViewPager) findViewById(R.id.pager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
}
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
...
}
#Override
protected void onResume() {
super.onResume();
//WORKS
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
//DOESN'T WORK
mMyFragmentPagerAdapter.notifyDataSetChanged();
mViewPager.setAdapter(mMyFragmentPagerAdapter);
}
}
Populate:
public void startView() {
this.questions.clear(); // this.questions is a LinkedList
this.questions.add(q);
}
First of all, to update a ListView / ViewPager or any other kind of view and it's adapter,you need to requery the Cursor for example if you are using it to populate the view. So as in your situation, you have a ViewPager and an Adapter. So the first thing which you consider is the way you update your LinkedList. In your situations you are doing it right, but in my opinion this.quetions in your external class is different than the LinkedList you are using to populate your adapter. In this case I would suggest you don't use external class to update your adapter. If that is too hard to achieve depending on the code organisation, just be sure that you are using the same LinkedList in your Fragment and external class.
Change your adapter extends FragmentPagerAdapter to FragmentStatePagerAdapter and then the notifyDataSetChanged work fine.
It's work for me.

How to get fragment references for Android FragmentPagerAdapter

I'm using a ViewPager with a FragmentPagerAdapter to allow swiping between 2 fragments. I'm not using FragmentStatePagerAdapter because it destroys and recreates new fragment instances when swiping between them, and I don't want operations in one of the Fragments executing more than once). I need a way to get references to the 2 created fragments to pass to the FragmentPagerAdapter, but the following code is producing an error (see below):
public class MyActivity extends FragmentActivity {
private ViewPager viewPager;
private MyPagerAdapter pagerAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
initViewPager();
}
private void initViewPager() {
List<Fragment> fragments = new ArrayList<Fragment>();
FragmentManager fragManager = getSupportFragmentManager();
fragments.add(fragManager.findFragmentById(R.id.fragment_1));
fragments.add(fragManager.findFragmentById(R.id.fragment_2));
pagerAdapter = new MyPagerAdapter(fragManager, fragments);
viewPager = (ViewPager) findViewById(R.id.fragment_pager);
viewPager.setAdapter(pagerAdapter);
}
...
...
private class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyPagerAdapter(FragmentManager fragmentManager, List<Fragment> fragments) {
super(fragmentManager);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
...
...
This produces the following error (or similar):
java.lang.IllegalStateException: Can't change container ID of fragment Fragment1{4169f780 #1 id=0x7f0b0005 android:switcher:2131427331:0}: was 2131427333 now 2131427331
Can anybody help me with this?
Edit
I've also tried the following, but it instantiates each fragment exactly twice (understandably), which I don't want. Other than that though, it works with this code:
private void initViewPager() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);
viewPager = (ViewPager) findViewById(R.id.fragment_pager);
viewPager.setAdapter(pagerAdapter);
}
Here is a very clear tutorial with sample code that I have used to build a viewpager layout.
As #harmanjd seems to suggest, you don't put the fragments in the XML file with the viewpager layout. Define each in its own XML and/or Java class, instantiate those like you have above (use fragments.add(Fragment.instantiate(this, CustomFragment.class.getName())) for a custom class), put them in pagerAdapter and give that to the ViewPager.
You ViewPager XML should look something like this (unless it's part of a larger layout besides the fragments):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ViewPager
android:id="#+android:id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>

Android:Null Pointer in ViewPager

I am making an app in which i am using ViewPAger and my code is working fine on S2 and NOTE but i checked on Y its giving exception on viewPagerAdapter. My code is as follows:
static PagerAdapter mPagerAdapter;
static ViewPager mViewPager;
static ViewPagerIndicator mIndicator;
static String addposition;
public Context _context=this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create our custom adapter to supply pages to the viewpager.
mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mPagerAdapter); // **Null Pointer Exception**
You can not write public Context _context=this; in global. You should initialize the content in onCreate().
Edit
Actually I had not seen that you are not using the context. I think there is an issue in the ViewPager. May be you are getting null object of ViewPager. If you show your xml then we can provide a better solution

Categories

Resources