Reputation: 89
I am trying to implement Google Map in android using Google API, but I am getting the error
The method getFragmentManager() is undefined for the type MainActivity
the whole MainActivity
code is as follows:
public class MainActivity extends Activity {
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
Upvotes: 5
Views: 19247
Reputation: 880
Fragments were available from Honey Comb onwards and hence your target API shall >= 11
If you want to use fragments to older versions of android you shall use android support v7 library. And in that case your MainActivity shall extend ActionBarActivity, instead of Activity.
If you are using android support v4, your MainActivity shall extend FragmentActivity and you will need to call getSupportFragmentManager() instead of getFragmentManager()
I hope it helps!
Upvotes: 8
Reputation: 2734
Make sure to add this import
import com.google.android.gms.maps.MapFragment;
And add this annotation before the onCreate method
@SuppressLint("NewApi")
Hope this will help !
Upvotes: 0
Reputation: 13520
If you want to use getFragmentManager()
you need to extend FragmentActivity
not Activity
like
public class MainActivity extends FragmentActivity {
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
Upvotes: 0