0

i'm trying to set Google Map on android so i have set all things on, but when i try to get the fragment that contain the map the error log display a NullPointerException error. the error is due to it's not recognize the map

My XML Layout :

<FrameLayout 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"
tools:context="com.example.unchainedappli.SpotFragement">

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

</FrameLayout>

The onCreate method

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get a handle to the Map Fragment
    GoogleMap map = ((SupportMapFragment) getFragmentManager()
            .findFragmentById(R.id.map)).getMap();

    LatLng sydney = new LatLng(-33.867, 151.206);

    map.setMyLocationEnabled(true);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

    map.addMarker(new MarkerOptions()
            .title("Sydney")
            .snippet("The most populous city in Australia.")
            .position(sydney));

}

The error log :

0-17 08:12:32.884    2373-2373/com.example.user.unchainedappli E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user.unchainedappli, PID: 2373
java.lang.NullPointerException
        at com.example.unchainedappli.SpotFragement.onCreate(SpotFragement.java:69)
        at android.support.v4.app.Fragment.performCreate(Fragment.java:1481)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:908)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
Karimx
  • 73
  • 1
  • 11
  • You have not mentioned API key for Google maps V2 xml layout – Harshad07 Oct 17 '14 at 08:38
  • but no it must be in AndroidManifiest. – Karimx Oct 17 '14 at 08:40
  • i said about the AndroidMainfest.xml file . Are you sure you have put you API key – Harshad07 Oct 17 '14 at 08:42
  • Yes. i'm the null pointer exception is not about this, but because an element is null and it's my google map this line `GoogleMap map = ((SupportMapFragment) getFragmentManager() .findFragmentById(R.id.map)).getMap();` – Karimx Oct 17 '14 at 08:44

2 Answers2

0

Well, The error clearly states that exception in SpotFragement.java file.line no 69. Go through that line.. sometimes you are passing null values to a method/function or retrieving an null values etc..

the hulk
  • 25
  • 7
  • this is the line `GoogleMap map = ((SupportMapFragment) getFragmentManager() .findFragmentById(R.id.map)).getMap();` – Karimx Oct 17 '14 at 08:41
  • Have you added the required permissions and Google Map API key in your manifest.xml file? – the hulk Oct 17 '14 at 08:46
  • Yes, i've said that i've set all things perfectly – Karimx Oct 17 '14 at 08:50
  • setcontentview(R.layout.YOURLAYOUTNAME); – the hulk Oct 17 '14 at 08:56
  • is a Fragment and not an activity. The fragment have the onCreateView method – Karimx Oct 17 '14 at 09:07
  • @KANDROIDOS the hulk is right. The error happens in the onCreate method of your activity because the activity has no layout set. So the fragment may be specified in your layout xml but your activity does not know about that. Thus findFragmentById() does not find it. Thus the NPE. – Ridcully Oct 17 '14 at 09:10
  • I’m talking about a fragment and not an activity. i put the google map in a fragment and not an activity. – Karimx Oct 17 '14 at 09:12
  • @KANDROIDOS fragement is in one particular layout right. So add setcontentview() in your oncreate() method and try it. – the hulk Oct 17 '14 at 09:18
  • to explain for the last time. i set the google map in a framgent and not an activity. So is a fragment that implement the google map fragment. The fragments have the onCreateView instead ofsetcontentview() – Karimx Oct 17 '14 at 09:20
  • Ok.. Try this one..mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); – the hulk Oct 17 '14 at 09:47
  • @KANDROIDOS this question in Stackoverflow might help you..http://stackoverflow.com/questions/16978190/add-google-maps-api-v2-in-a-fragment – the hulk Oct 17 '14 at 10:28
0

Try this :

FragmentManager frag = getActivity().getSupportFragmentManager();

final SupportMapFragment myfrag = (SupportMapFragment) frag 
            .findFragmentById(R.id.map);
Harshad07
  • 608
  • 7
  • 23