0
  import android.app.Activity;
  import android.os.Bundle;
  import android.support.v4.app.FragmentActivity;
 import android.view.Menu;
  import android.view.View;
  import android.widget.Toast;

 import com.google.android.gms.common.ConnectionResult;
 import com.google.android.gms.common.GooglePlayServicesUtil;
 import com.google.android.gms.maps.CameraUpdate;
 import com.google.android.gms.maps.CameraUpdateFactory;
 import com.google.android.gms.maps.GoogleMap;
 import com.google.android.gms.maps.MapFragment;
 import com.google.android.gms.maps.model.LatLng;
 import com.google.android.gms.maps.model.MarkerOptions;

 public class GymFind extends Activity {

  // gets stuck on any call to LatLng   static final LatLng LOCATION_BRAY = new LatLng(53.183304,-6.123726);
static final LatLng LOCATION_Greystones = new LatLng(53.136525,-6.065014);
static final LatLng LOCATION_Charlesland = new LatLng(53.122311,-6.064846);
private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.gmaps);


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

      map.addMarker(new MarkerOptions().position(LOCATION_BRAY).title("Find us here!"));
      map.addMarker(new MarkerOptions().position(LOCATION_Greystones).title("Find us here!"));
      map.addMarker(new MarkerOptions().position(LOCATION_Charlesland).title("Find us here!"));
}

layout:

  <RelativeLayout 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=".MainActivity" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Hello World" />
<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment"/>

 </RelativeLayout>

logcat :

    07-24 13:33:57.423: E/AndroidRuntime(1353): FATAL EXCEPTION: main
      07-24 13:33:57.423: E/AndroidRuntime(1353): java.lang.RuntimeException: Unable to      start activity 
      java.lang.NullPointerException
     07-24 13:33:57.423: E/AndroidRuntime(1353):    at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
     07-24 13:33:57.423: E/AndroidRuntime(1353):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
     07-24 13:33:57.423: E/AndroidRuntime(1353):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
     07-24 13:33:57.423: E/AndroidRuntime(1353):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    07-24 13:33:57.423: E/AndroidRuntime(1353): at android.os.Handler.dispatchMessage(Handler.java:99)
     07-24 13:33:57.423: E/AndroidRuntime(1353): at  android.os.Looper.loop(Looper.java:137)
    07-24 13:33:57.423: E/AndroidRuntime(1353): at  android.app.ActivityThread.main(ActivityThread.java:5041)
    07-24 13:33:57.423: E/AndroidRuntime(1353): at  java.lang.reflect.Method.invokeNative(Native Method)
    07-24 13:33:57.423: E/AndroidRuntime(1353): at  java.lang.reflect.Method.invoke(Method.java:511)
    07-24 13:33:57.423: E/AndroidRuntime(1353): at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
   07-24 13:33:57.423: E/AndroidRuntime(1353):  at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    07-24 13:33:57.423: E/AndroidRuntime(1353): at   dalvik.system.NativeStart.main(Native Method)
     07-24 13:33:57.423: E/AndroidRuntime(1353): Caused by: java.lang.NullPointerException
     07-24 13:33:57.423: E/AndroidRuntime(1353): at   com.thedesignpool.shorelineleisure.GymFind.onCreate(GymFind.java:35)
     07-24 13:33:57.423: E/AndroidRuntime(1353): at android.app.Activity.performCreate(Activity.java:5104)
     07-24 13:33:57.423: E/AndroidRuntime(1353): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
     07-24 13:33:57.423: E/AndroidRuntime(1353): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

2 Answers2

1

This is because you are trying to get map field right away after setting content view, but it would be available only after fragment's onCreateView() call, so you should deffer getting map and operating on it till onCreateView().

Desert
  • 2,293
  • 15
  • 16
  • thank you ever so much guys.so now it says I need to update my google play services so I will do that. Also it's not letting me override my onCreateView function, where should I write the map code. – Ivan Razimov Jul 25 '13 at 15:42
  • You have to extend `com.google.android.gms.maps.MapFragment` and override it there. – Desert Jul 25 '13 at 15:57
0

Use:

map = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

instead of this:

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • It won't let me use the getSupportFragmentManager function. It says cannot cast from fragment to mapfragment. so do I have to change what my class extends and what do I extend? Thank you very much. – Ivan Razimov Jul 25 '13 at 16:13