0

I have a MainActivity which has an ActionBar with two tab, the first tab is to show the sensor data value and the second tab I want to show the Map. But I have a problem to implement the map in the fragment on the second tab

The MainActivity is shown as below:

public class MainActivity extends Activity
{
private static final String DATA_FRAGMENT_BAR = "Data";
private static final String MAP_FRAGMENT_BAR = "Map";

private DataCollection mDataCollection;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Config.mContext = this;
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME| ActionBar.DISPLAY_SHOW_CUSTOM);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab dataTab = actionBar.newTab().setText(DATA_FRAGMENT_BAR);
    ActionBar.Tab mapTab = actionBar.newTab().setText(MAP_FRAGMENT_BAR);
    Fragment dataFragment = new FragmentData();
    Fragment mapFragment = new FragmentMap();

    dataTab.setTabListener(new MyTabsListener(dataFragment));
    mapTab.setTabListener(new MyTabsListener(mapFragment));

    actionBar.addTab(dataTab);
    actionBar.addTab(mapTab);
    mDataCollection = DataCollection.getInstance();
}
....
}

The TabsListener is shown as below public class MyTabsListener implements ActionBar.TabListener { private Fragment fragment;

public MyTabsListener(Fragment fragment)
{
    this.fragment = fragment;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    ft.replace(R.id.fragment_container, fragment);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    ft.remove(fragment);
}
}

The the map fragment class is shown as below:

public class FragmentMap extends Fragment
{

MapView mapView;
GoogleMap map;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    View v = inflater.inflate(R.layout.fragment_map, container, false);
    // Gets the MapView from the XML layout and creates it
    mapView = (MapView) v.findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff
    map = mapView.getMap();
    //map.getUiSettings().setMyLocationButtonEnabled(false);
    //map.setMyLocationEnabled(true);
    map.addMarker(new MarkerOptions().position(new LatLng(50.167003,19.383262)));


    // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
    try 
    {
        MapsInitializer.initialize(this.getActivity());
    } 
    catch (GooglePlayServicesNotAvailableException e)
    {
        e.printStackTrace();
    }

    // Updates the location and zoom of the MapView
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
    map.animateCamera(cameraUpdate);

    return v;
}

@Override
public void onResume() 
{
    mapView.onResume();
    super.onResume();
}

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

@Override
public void onLowMemory()
{
    super.onLowMemory();
    mapView.onLowMemory();
}
}

I set a break point in the FragmentMap class at the line "mapView.onCreate(savedInstanceState);" the savedInstanceState is null and the program will crash after line.

Can someone help. Thanks in advance.

user2753594
  • 145
  • 4
  • 15

2 Answers2

0

Google released the Map API Version 2. This gives you a MapFragment and a SupportMapFragment. This allows you to add a Map without extending MapActivity.

Google Maps v2 In fragment cashes when clicked twice

Community
  • 1
  • 1
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99
  • 1
    I have add the TabsListener class on the question. My problem is if I used the MapFragment, the TabsListener will not work. I have two tabs, one of the tabs is to show the sensors value, so I used Fragment – user2753594 Oct 14 '13 at 12:12
0

It is not really clear in the documentation, but the MapView.onCreate() method should be called in the Fragment.onActivityCreated(), not in Fragment.onCreate()

Also, MapView.getMap() can return null, don't forget to check with that.

And you should call super.onResume() before mapView.onResume()

nicopico
  • 3,606
  • 1
  • 28
  • 30