1

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.

Zach
  • 9,989
  • 19
  • 70
  • 107

1 Answers1

1

This is how I implemented it using MapView. Hope this will be helpful

Xml for fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Java Code

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_lugar,container,false);
    latitude = 26.78;
    longitude = 72.56;
    mMapView = (MapView) view.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume();// needed to get the map to display immediately

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    googleMap = mMapView.getMap();
    // latitude and longitude
    double latitude = 17.385044;
    double longitude = 78.486671;

    // create marker
    MarkerOptions marker = new MarkerOptions().position(
            new LatLng(latitude, longitude)).title("Hello Maps");

    // Changing marker icon
    marker.icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

    // adding marker
    googleMap.addMarker(marker);
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(17.385044, 78.486671)).zoom(12).build();
    googleMap.animateCamera(CameraUpdateFactory
            .newCameraPosition(cameraPosition));
    return view;
}
/***** Sets up the map if it is possible to do so *****/
@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}
Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
  • then how do you put that in a viewpager? – iOSAndroidWindowsMobileAppsDev Oct 12 '16 at 13:50
  • 1
    @Jquery Ninja simply put a map view in your fragment layout and add that fragment in View Pager – Vivek Mishra Oct 12 '16 at 15:54
  • @JqueryNinja it is the same code that I have posted – Vivek Mishra Oct 13 '16 at 06:46
  • the part I'm failing to understand is how to connect. I have something similar to your answer in an activity and in a fragment. Both work. It's just putting in a ViewPager which is the problem. There are no imports, no class declarations. I can't see how the xml and java are connected. Please – iOSAndroidWindowsMobileAppsDev Oct 13 '16 at 07:06
  • If you have created adapter for your view pager then just simply show your fragment when the position matches and if not then follow this link to create adapter for your view pager. http://stackoverflow.com/questions/18413309/how-to-implement-a-viewpager-with-different-fragments-layouts – Vivek Mishra Oct 13 '16 at 07:10
  • @JqueryNinja they are connected via `MapView` defined in xml layout for showing maps – Vivek Mishra Oct 14 '16 at 07:27