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];
}
}
getActivity() returns null in Fragment function. Bookmark this question. Show activity on this post. and yes when I call it (from the Activity), it is null...
onAttach(Activity) called once the fragment is associated with its activity. onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
Your fragment has probably been detached from the activity. See this link for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With