1

friends, I am getting NullPointer error ,on addMarker()

Below is my sample code.

GoogleMap myMap;
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment myMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
myMap.setMyLocationEnabled(true);
myMap.getUiSettings().setZoomControlsEnabled(true);
myMap.getUiSettings().setCompassEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true);

Error message like NullPointer at line

myMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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"
    android:orientation="vertical"
    tools:context=".DemoMapActiviy" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

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

any idea how can i solve it?

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133

1 Answers1

0

I went through this issue a while back. First check to see if your fragment is found by the fragment manager.

If not try replacing the way you've put your fragment into your view. If you want an example of another way I've posted a maps example here

Any more questions feel free to comment and I'll work through it with you :)

EDIT

have you got the correct API key in there? If you can't contact Google's servers then there's a good chance that that's your problem. Otherwise may be a manifest issue make sure you have READ_GSERVICES and INTERNET permissions.

See Google Maps API V2 'Failed to Load Map. Could not contact Google Servers'

Edit 2

Ok so I was being lazy before and just referring you to my project. If you were getting a connection to maps in your project lets start from there.

What I was suggesting in my original response was to check and see if myMapFragment was null. If it is great try doing something along the lines of the following:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SupportMapFragment myMapFragment = new SupportMapFragment();
ft.replace(R.id.map, myMapFragment, Constant.TAXI_FRAGMENT_ID);
ft.commit();

Where R.id.map is an empty FrameLayout

If myMapFragment isn't null then try extending SupportMapFragment and running the following test:

public class MyMapFragment extends SupportMapFragment {
    @Override
    public void onActivityCreated()
    {
        if (getMap() == null) Log.e("TAG", "There's something very wrong here";
    }
}

Also if you do this make sure that you are inflating an instance of MyMapFragment not of SupportMapFragment

Community
  • 1
  • 1
chris-tulip
  • 1,840
  • 1
  • 15
  • 22
  • i check your example and varify it with my application now issues is when i run application error message give like Failed to load map. Could not contact Google servers. –  Feb 04 '13 at 10:43