0

I have a master class which creates two fragment classes. One of them is MapDisplay().java

I want to display a map in this class. Here is my code for MapDisplay()

public class MapDisplay extends Fragment implements OnMapClickListener{
    private GoogleMap googlemap;
    LatLng GAN = new LatLng(34.4305556,74.9250000);

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.maplayout, container, false);


        try
        {
            if(googlemap==null)
            {
                 googlemap=  
            ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map2)).getMap();
            googlemap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            googlemap.setMyLocationEnabled(true);
            googlemap.getUiSettings().setMyLocationButtonEnabled(true);
            googlemap.getUiSettings().setZoomControlsEnabled(false);

            }
            if(googlemap!=null)
            {

            Marker m = googlemap.addMarker(new MarkerOptions().position(GAN).title("Gangbal")); 
            m.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
            googlemap.moveCamera(CameraUpdateFactory.newLatLngZoom(GAN, 15));
            googlemap.animateCamera(CameraUpdateFactory.zoomTo(10),2000,null);
            googlemap.setOnMapClickListener(this);
            }
            else
            {
                Toast.makeText(getActivity(), "WELL! HELLO THERE", Toast.LENGTH_LONG).show();
            }       
        } catch(Exception e)
        {
            e.printStackTrace();
        }
        return rootView;
    }

    @Override
    public void onMapClick(LatLng arg0) {
        Bundle args1 = new Bundle();
        args1.putParcelable("LOCATION", GAN);
        Intent a = new Intent(getActivity(),FullScreenMap.class);
        a.putExtra("bundle", args1);
        a.putExtra("title", "Gangbal");
        startActivity(a);

    }
}

And here's my XML layout file maplayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />



</RelativeLayout> 

This class however, doesn't display the map I want to. Just a world map with no markers. Also clicking on this map doesn't work.

I'm creating the MapDisplay() fragment class in another class

    FragmentPagerAdapter tabadapter = new TabAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager)findViewById(R.id.tabpager);
pager.setAdapter(tabadapter);

    class TabAdapter extends FragmentPagerAdapter{
public TabAdapter(FragmentManager fm){
    super(fm);
}

@Override
     public Fragment getItem(int m) {
    switch(m)
    {
    case 0:
        return new MapDisplay();
    case 1:
        return new Description();
    }
    return null;
}

    @Override
    public CharSequence getPageTitle(int position) {
        return CONTENT[position % CONTENT.length];
    }

    @Override
    public int getCount() {
      return CONTENT.length;
    }

}

So I guess MapDisplay() should extend Fragment, right?? What am I doing wrong? Please help!

MAK
  • 1,250
  • 21
  • 50
  • Have you tried to extend MapFragment instead of Fragment ? – Boban S. Apr 01 '14 at 14:33
  • You are doing it completely wrong, You need to extend `MapFragment` instead of `Fragment`, you also should not be using `SupportMapFragment` when you are using `MapFragment` in your xml and you do not get a Fragment by id in a fragment, you only do that in an activity – tyczj Apr 01 '14 at 14:35
  • @tyczj. Check the edited question. I'm creating fragment classes – MAK Apr 01 '14 at 14:55

1 Answers1

0

You need to use a MapView or your fragment should extend MapFragment.

http://developer.android.com/reference/com/google/android/gms/maps/MapView.html

So have the below in maplayout.xml

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

<com.google.android.gms.maps.MapView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    />

</RelativeLayout>

In Fragment

View rootView = inflater.inflate(R.layout.maplayout, container, false);
MapView  mapView = (MapView) rootView.findViewById(R.id.map);
GoogleMap map = mapView.getMap();

You also need to check the availability of google play services which is not shown here.

Note:

Users of this class (MapView) must forward all the life cycle methods from the Activity or Fragment containing this view to the corresponding ones in this class. In particular, you must forward on the following methods:

onCreate(Bundle)
onResume()
onPause()
onDestroy()
onSaveInstanceState()
onLowMemory()

You can check the answer @

Android - android.view.InflateException: Binary XML file line #8: Error inflating class fragment

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256