FragmentManager is always null - android

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.

Related

Android 'FragmentManager is already executing transactions' when notifydatasetchanged runs on ui thread

I'm using a TabLayout with ViewPager. I have two tabs in my tab layout which both have a different instances of the same fragment object. Both fragments have the same recycler view layout as well.
Problem: When I load the entire activity after getting the server objects back, I'm trying to populate both tab views with the new object, but I get the following error:
java.lang.IllegalStateException: FragmentManager is already executing
transactions
. So in my main activity:
public class MyMainActivity extends FragmentActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private MyAdapter adapter;
private MyFragment fragmentA;
private MyFragment fragmentB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
tabLayout = (TabLayout)findViewById(R.id.tab_layout);
viewPager = (ViewPager)findViewById(R.id.view_pager);
adapter = new MyAdapter(getSupportFragmentManager(), new
ArrayList<>(), new ArrayList<>());
// Instantiate the fragments here
adapter.addFragment(....A, "Title A");
adapter.addFragment(....B, "Title B");
.....
.....
.....
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
public void refreshPage() {
runOnUiThread(new Runnable() {
#Override
public void run() {
// This line crashes
adapter.notifyDataSetChanged();
}
});
}
}
I tried using getChildFragmentManager but I can't because you need to call that via a fragment object. I cannot show much code as most of it is proprietary and I had to make the code as generic as possible.
When I comment out the line that crashes, my recycler view for the first tab does not refresh unless I scroll. while the second tab does not refresh at all. Did anyone have the same issue before? How did you resolve it?
P.S I'm not using any FragmentManager or transactions so I'm not sure why it gives me that exception.

Helpful Sherlock Fragment ListView examples or advice

I'm looking for some ideas on how to do a simple listview with images while inside a SherlockFragment class; Which is then loaded into a ViewPager fragment. I've tried several examples listed below, each one I cannot get to successfully work. I'm not asking for someone to do it for me, I'm looking for advice on how to build one from someones experience to direct me for resources.
Examples i have tried
android fragment example
android sdk fragments
I assume that the listview with images is no problem.
To show fragments in a viewpager you should work with the support-library and go like this:
you create a layout with a viewpager:
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
create a fragment that inflates your layout in 1., your on onCreateView Method should conatin the following lines:
View v = inflater.inflate(R.layout.view_pager_layout, parent, false);
MyFragmentAdapter mMyPagerAdapter =
new MyFragmentAdapter (getChildFragmentManager());
// ... set data to pager
mMyPagerAdapter.setCountOfFragments(mCountOfFragments);
mViewPager = (ViewPager) v.findViewById(R.id.view_pager);
mViewPager.setAdapter(mMyPagerAdapter);
3.create a class that extends from FragmentStatePagerAdapter. This should look like this:
public class MyFragmentAdapter extends FragmentStatePagerAdapter{
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int position) {
// create here you listfragment that should be shown in your viewPager
Fragment fragment = new ...;
// you also can set data to this listFragment
Bundle bundle = new Bundle();
fragment.setArguments(bundle);
return fragment;
}
#Override
public int getCount() {
return mCountOfFragments;
}
}
These Links were helpfull for me:
http://developer.android.com/training/animation/screen-slide.html
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
And also the sample of effectivenavigation (https://android.googlesource.com/platform/development/+/d80ee02/samples/training/EffectiveNavigation) is really helpful.

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.

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

FragmentActivity not calling ListFragment

I am trying to implement a ListFragment. The idea is to use CursorLoader. The code for FragmentActivity is
public class XYZ extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getSupportFragmentManager();
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
XYZFragment list = new XYZFragment();
fm.beginTransaction().add(android.R.id.content, list).commit();
}
}
Now I assume that onActivityCreate of XYZFragment should be called but that is not happening instead I am getting a the below image on the Emulator. I am looking for an explanation as to what is happening and what I am doing wrong?
Thanks.
Change onActivityCreate to onActivityCreated and you should get the expected results.

Categories

Resources