Reputation: 39538
I currently have a thread in my main Activity do download stuff frow web and I then need to update the Fragments inside a ViewPager after download finished.
The download is handled by a service and broadcast an Intent when finished.
So, basically, my code in my main Activity is:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
((PositionFragment)mPagerAdapter.getItem(0)).updateUI();
}
}
and my PositionFragment:
public void updateUI() {
mActivity = getActivity();
I really don't get how this can be null. This really souds simple, but I must be missing something!
Any idea?
Edit: my Adapter:
public class PageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public PageAdapter(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
Upvotes: 5
Views: 27905
Reputation: 6517
You can call getActivity()
only after fragment's onAttach
method is called. I believe, that's the issue here.
Upvotes: 0