我正在开发一个显示具有指定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();
}
}
GoogleMapFragment Class =
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$3.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)