0

I tried to call getMap() in my application and always get error at the line

map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

The activity for displaying the GoogleMap is implement as a fragment. What could be wrong? My whole activity is shown below.

 package sg.SanThit.TrackMe;

 import android.annotation.SuppressLint;
 import android.database.Cursor;
 import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.Toast;

 import com.google.android.gms.maps.GoogleMap;
 import com.google.android.gms.maps.SupportMapFragment;

 public class MyDetailsFragment extends Fragment {
 private TrackerInfo trackerDetailInfo;
 GoogleMap map;



public MyDetailsFragment() {    
}

@SuppressLint("NewApi")
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
        map = ((SupportMapFragment)      
        getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        if (map == null) {
            Toast.makeText(getActivity(), "Google Maps not available", 
    Toast.LENGTH_LONG).show();
        }
}}
Bryanyan
  • 677
  • 3
  • 13
  • 30

1 Answers1

0

Use FragmentActivity instead of public class MyDetailsFragment extends Fragment.

Below is sample code that loads a map :

  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_map);

   map = ((SupportMapFragment)      
  getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
     }

Here R.layout.activity_map is the XML file containing fragment for map.

Divya Motiwala
  • 1,659
  • 1
  • 16
  • 24
  • When I change to FragmentActivity I have problem at fragment transaction from the main activity to the detail activity. At Fragment fragment = new MyDetailsFragment(); – Bryanyan Mar 29 '13 at 08:22
  • Since now your `DetailsFragment` class extentds `FragmentActivity`, change it to `FragmentActivity fragment = new MyDetailsFragment();`. – Divya Motiwala Mar 29 '13 at 08:53