Android 如何将标记添加到MapView

Android 如何将标记添加到MapView,android,google-maps,marker,Android,Google Maps,Marker,基本上,我使用mapview在应用程序中显示地图。意味着不像大多数地图应用程序那样使用片段。但是我面临一个问题,要在地图上添加一个标记。请帮帮我 这是我的代码 private static final int GPS_ERROR_DIALOG_REQUEST = 9001; MapView mMapView; GoogleMap map; Context CTX = this; @Override protected void onCreate(Bundle savedInstanceStat

基本上,我使用mapview在应用程序中显示地图。意味着不像大多数地图应用程序那样使用片段。但是我面临一个问题,要在地图上添加一个标记。请帮帮我

这是我的代码

private static final int GPS_ERROR_DIALOG_REQUEST = 9001;
MapView mMapView;
GoogleMap map;
Context CTX = this;

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

    if (servicesOK()) {
        setContentView(R.layout.parents);
        mMapView = (MapView) findViewById(R.id.map);
        mMapView.onCreate(savedInstanceState);
        Toast.makeText(this, "Ready to map!", Toast.LENGTH_SHORT).show();
        map.addMarker(new MarkerOptions()
                .position(new LatLng( 4.588946, 101.125293))
                .title("You are here"));
    }
    else
    {
        Toast.makeText(this, "Not ready to map!", Toast.LENGTH_SHORT).show();
    }

    Button AddUser = (Button) findViewById(R.id.user_reg);
    Button KidsDelete = (Button) findViewById(R.id.user_del);

    AddUser.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(Parents.this, AddUser.class));
        }
    });
    KidsDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(Parents.this, KidsDelete.class));
        }
    });
}

public boolean servicesOK() {

    int isAvailable = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(CTX);

    if (isAvailable == ConnectionResult.SUCCESS) {
        return true;
    } else if (GoogleApiAvailability.getInstance().isUserResolvableError(isAvailable)) {
        Dialog dialog = GoogleApiAvailability.getInstance().getErrorDialog(this, isAvailable, GPS_ERROR_DIALOG_REQUEST);
        dialog.show();
    } else {
        Toast.makeText(this, "Can't connect to Google Play services", Toast.LENGTH_SHORT).show();
    }
    return false;

}

//For mapView
@Override
protected void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}

@Override
protected void onPause() {
    super.onPause();
    mMapView.onPause();
}


@Override
protected void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
protected void onSaveInstanceState(Bundle outstate) {
    super.onSaveInstanceState(outstate);
    mMapView.onSaveInstanceState(outstate);
}
这就是错误所在

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.moon.mykidstracker, PID: 3031
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.moon.mykidstracker/com.example.moon.mykidstracker.Parents}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
                      at com.example.moon.mykidstracker.Parents.onCreate(Parents.java:35)
                      at android.app.Activity.performCreate(Activity.java:6237)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

在活动/片段中实施
OnMapReadyCallback

比onMapReady方法好

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        if (mMap != null) {
            marker = mMap.addMarker(new MarkerOptions()
                    .position(latLng).title(place_name)
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN))
                    .draggable(false).visible(true));
        }
    }

你的
谷歌地图
是否为空如何修复@PiyushVar正在初始化map。您刚刚声明为GoogleMap;我使用getmapasync(),标记就在那里,谢谢!:在我添加map=yourMapView.getMapAsync(this);,之后,Dit已解决;。谢谢:D
   -----------------------
         map = yourMapView.getMap();