向动态 Google 地图添加标记

Add a marker to a dynamic Google Map

我正在开发一个显示具有指定 LatLng 值的地图的应用程序。 地图片段是动态的(不是R.id.map)

当我尝试将标记添加到指定位置时(我从 Google Places API JSON 请求获得)我得到一个空指针执行。

这是代码:

        @Override
public void initMap(int position, long id, ListView listView) {
    Cursor cursor = (Cursor) listView.getItemAtPosition(position);
    String lat = cursor.getString(cursor.getColumnIndexOrThrow(PlacesContract.Places.LAT));
    String lng = cursor.getString(cursor.getColumnIndexOrThrow(PlacesContract.Places.LNG));
    String name = cursor.getString(cursor.getColumnIndexOrThrow(PlacesContract.Places.NAME));
    LatLng latLng = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
    GoogleMapOptions options = new GoogleMapOptions();

    options.mapType(GoogleMap.MAP_TYPE_HYBRID);

    options.camera(CameraPosition.fromLatLngZoom(latLng, 15));

    mapFragment = GoogleMapFragment.newInstance(options);
    mUIGoogleMap.addMarker(new MarkerOptions().position(latLng).title(name));
    if (findViewById(R.id.map_container) != null) {
        getSupportFragmentManager().beginTransaction().replace(R.id.map_container, mapFragment).addToBackStack(null).commit();
    } else {
        getSupportFragmentManager().beginTransaction().replace(R.id.listview_container, mapFragment).addToBackStack(null).commit();

}
}

GoogleMapFragmentClass=

public class GoogleMapFragment extends SupportMapFragment {

private static final String SUPPORT_MAP_BUNDLE_KEY = "MapOptions";

public static interface OnGoogleMapFragmentListener {
    void onMapReady(GoogleMap map);
}

public static GoogleMapFragment newInstance() {
    GoogleMapFragment fragment = new GoogleMapFragment();
    return fragment;
}

public static GoogleMapFragment newInstance(GoogleMapOptions options) {
    Bundle arguments = new Bundle();
    arguments.putParcelable(SUPPORT_MAP_BUNDLE_KEY, options);

    GoogleMapFragment fragment = new GoogleMapFragment();
    fragment.setArguments(arguments);
    return fragment;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {
        mCallback = (OnGoogleMapFragmentListener) getActivity();
    } catch (ClassCastException e) {
        throw new ClassCastException(getActivity().getClass().getName() + " must implement OnGoogleMapFragmentListener");
    }
}

@Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
    View v = super.onCreateView(arg0, arg1, arg2);
    Fragment fragment = getParentFragment();
    return v;
}

private OnGoogleMapFragmentListener mCallback;

}

错误日志:

03-01 13:40:36.069  17720-17720/com.fragments.ofir.nearby E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.fragments.ofir.nearby, PID: 17720
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.fragments.ofir.nearby.MainActivity.initMap(MainActivity.java:184)
        at com.fragments.ofir.nearby.fragments.ResultsFragment.onItemClick(ResultsFragment.java:113)
        at android.widget.AdapterView.performItemClick(AdapterView.java:300)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1186)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3095)
        at android.widget.AbsListView.run(AbsListView.java:4038)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5274)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

与其创建 SupportMapFragment 的新实例,不如尝试扩展 SupportMapFragment class。检查 this.