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!"));
}

布局:

  <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>

日志猫:

    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)
4

2 回答 2

1

这是因为您试图map在设置内容视图后立即获取字段,但它只有在片段onCreateView()调用后才可用,因此您应该将获取map和操作它推迟到onCreateView().

于 2013-07-24T22:26:14.877 回答
0

利用:

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

而不是这个:

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
于 2013-07-24T22:27:50.010 回答