I am trying to load a Mapfragment inside a PagerAdapter. Here is teh adapter code,
public class ShopPagerAdapter extends PagerAdapter {
private List<LoyaltyOutlet> data;
private Context context;
FragmentManager fragmentManager;
public ShopPagerAdapter(List<LoyaltyOutlet> data, Context context,FragmentManager manager) {
this.data = data;
this.context = context;
this.fragmentManager = manager;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
LoyaltyOutlet outlet = data.get(position);
LayoutInflater inflater = LayoutInflater.from(context);
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.fragment_loyalty_shop_item, collection, false);
TextView address = (TextView)layout.findViewById(R.id.txt_ShopAddress);
GoogleMap googleMap = ((SupportMapFragment) fragmentManager.findFragmentById(R.id.shopMap)).getMap();
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
googleMap.animateCamera(cameraUpdate);
address.setText(outlet.getOutletName());
collection.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return data.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
return "";
}
}
But when I run code I am getting an exception,
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
Is it possible to use the MapFragment here or should I move this code inside the fragment lifecycle methods?
Thanks.