2

I'm getting an NullPointerException with the following code:

private ProjectionProxy proxy = new ProjectionProxy();
private GoogleMap mMap = null;
private Context context = this;

@Override
protected void onCreate(Bundle load) { 
    super.onCreate(load);

    setContentView(R.layout.fragment_ride_tracking);

    //mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.myMapView)).getMap();

    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);

    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mMap.setMyLocationEnabled(true);

    //SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView);
    //mMap = mapFragment.getMap();

    Log.e("RideTracking", "Google Map VALUE:"+mMap);

    if (mMap != null) { 
        proxy.setProjection(mMap.getProjection());
    }

The line where the NullPointerException is happening is this line of code:

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

I'm not initializing my mMap variable to null. What is my issue here?

4
  • Are you using Support library? Commented May 16, 2013 at 15:37
  • Yes, I am using the Support library Commented May 16, 2013 at 15:42
  • Not sure but Try this mMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap(); Commented May 16, 2013 at 15:49
  • 1
    Are you sure there's a valid fragment in your layout? Seems like the fragment that's retrieved by "findFragment" is null. But without the layout file and without a stack trace, there's not much I can say. Commented May 16, 2013 at 15:49

3 Answers 3

7

MainActivity.xml :

<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.SupportMapFragment"/>

MainActivity.java :

private GoogleMap googleMap;
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Sign up to request clarification or add additional context in comments.

2 Comments

This is not a complete answer. If you know what is wrong with OP's code, please elaborate.
GigoNet Gigolashvili answer is correct. Importent difference is:If you are using API level < 12, use SupportMapFragment instead of MapFragment in code and xml.
4

Try to check the MapFragment that you've created in the XML of the Activity. Check mine; it's working.

...

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

...

Is yours different? Also, you checked if you put the correct permissions and metadata on the AndroidManifest.xml?

Here's the permission:

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

And here's the metadata section:

<application>

    ...

    <meta-data android:name="com.google.android.maps.v2.API_KEY"
               android:value="HERE GOES YOUR API KEY" />

</application>

I hope this helps you!

Comments

0

You may be missing this from manifest.xml
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.