Ivan Razimov
Ivan Razimov

Reputation: 1

When trying to use google maps api calls I get runtime exception caused by nullpointer exception

  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)

Upvotes: 0

Views: 322

Answers (2)

Eng.Fouad
Eng.Fouad

Reputation: 117587

Use:

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

instead of this:

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

Upvotes: 0

Desert
Desert

Reputation: 2303

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().

Upvotes: 1

Related Questions