我在片段中的以下代码执行时获得numm指针异常。
MapFragment mapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container1, mapFragment);
fragmentTransaction.commit();
mGoogleMap=mapFragment.getMap();
mGoogleMap.addMarker(new MarkerOptions().draggable(true).position(new LatLng(22.03, 72.56)).title("hello world").snippet("Ahmedabad").icon(BitmapDescriptorFactory.fromResource(R.drawable.wifi)));
03-01 15:59:49.370:E / AndroidRuntime(12577):引起: java.lang.NullPointerException 03-01 15:59:49.370: E / AndroidRuntime(12577):at com.tfick.fragment.PoiFragment.onCreateView(PoiFragment.java:64)03-01 15:59:49.370:E / AndroidRuntime(12577):at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829) 03-01 15:59:49.370:E / AndroidRuntime(12577):at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035) 03-01 15:59:49.370:E / AndroidRuntime(12577):at android.app.BackStackRecord.run(BackStackRecord.java:635)03-01 15:59:49.370:E / AndroidRuntime(12577):at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
答案 0 :(得分:0)
我已经解决了问题。这是解决方案Google Maps Android API v2 - Add a marker failed
答案 1 :(得分:0)
似乎getMap()
返回null
。
可能的解决方案:
GooglePlayServicesUtil.isGooglePlayServicesAvailable(context)
检查了Google Play服务是否可用?getMap()
,例如onResume()
答案 2 :(得分:0)
这是我找到的问题的解决方案。
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.tfick.android.R;
public class PoiFragment extends MapFragment {
/*object of google map*/
public GoogleMap mGoogleMap;
/* summy location*/
private static final LatLng SYDNEY = new LatLng(23.03, 72.62);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
if (getMap() != null) {
Log.d(getClass().getSimpleName(), "Map ready for use!");
mGoogleMap = getMap();
/* cretaing custom camera position */
CameraPosition mCameraPosition = new CameraPosition.Builder()
.target(SYDNEY).bearing(0).tilt(30).build();
Log.i("gmap", mGoogleMap.toString());
/* setting custom camera position */
mGoogleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(mCameraPosition));
/* adding custom marker on the map. */
mGoogleMap
.addMarker(new MarkerOptions()
.draggable(true)
.position(new LatLng(22.03, 72.56))
.title("hello world")
.snippet("Ahmedabad")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.wifi)));
mGoogleMap
.addMarker(new MarkerOptions()
.draggable(true)
.position(new LatLng(23.03, 78.10))
.title("hello world")
.snippet("Baroda")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.wifi)));
}
}
}