我的问题是此代码中的空指针异常:
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_maps, null, false);
FragmentTransaction ft = getFragmentManager().beginTransaction();
mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
SupportMapFragment fmap = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
if (fmap == null) {
fmap = SupportMapFragment.newInstance();
ft.add(R.id.map, fmap);
}
ft.commit();
正是在这一点上:
mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
我已经测试了某种类型的解决方案,但都不起作用。
发布我片段的所有代码:
public class mapsFragment extends SupportMapFragment {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
LocationManager manager;
Location loca;
String lati;
String longi;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_maps, null, false);
FragmentTransaction ft = getFragmentManager().beginTransaction();
mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
SupportMapFragment fmap = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
if (fmap == null) {
fmap = SupportMapFragment.newInstance();
ft.add(R.id.map, fmap);
}
ft.commit();
manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
String provider = manager.getBestProvider(criteria,true);
manager.requestLocationUpdates(provider, 0, 0, locationListener);
loca = manager.getLastKnownLocation(provider);
if(loca != null) {
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(loca.getLatitude(), loca.getLongitude())) // Sets the center of the map to location user
.zoom(16) // Sets the zoom
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
}
return rootView;
}
private final LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
insertMarker(location);
}
};
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
}
public void insertMarker(Location location) {
if(location != null){
double lat = location.getLatitude();
double lng = location.getLongitude();
lati = String.valueOf(lat);
longi = String.valueOf(lng);
mMap.addMarker(new MarkerOptions().position(new LatLng(lat, lng)).title(lati + " , " + longi));
}else{
lati = "Latitudine non trovata";
longi = "Longitudine non trovata";
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Il tuo GPS è disabilitato, vuoi attivarlo?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
getActivity().finish();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
}
全部谢谢:)
答案 0 :(得分:-2)
您可能希望将该行更改为
mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
或
mMap =((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
因为您应该在活动中调用getFragmentManager()
。