Google maps mMap=findFragmentById(R.id.map)).getMap();don';t在片段中工作并给出null指针
我的问题是此代码中存在空指针异常:Google maps mMap=findFragmentById(R.id.map)).getMap();don';t在片段中工作并给出null指针,google-maps,android-fragments,nullpointerexception,google-maps-api-2,Google Maps,Android Fragments,Nullpointerexception,Google Maps Api 2,我的问题是此代码中存在空指针异常: public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_maps, null, false); FragmentTransaction ft = getFragmentManager().beginTr
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();
}
公共类mapsFragment扩展了SupportMapFragment{
私有GoogleMap mMap;//如果Google Play services APK不可用,则可能为空。
地点经理;
位置失水事故;
弦纬度;
弦长;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图根视图=充气器。充气(R.layout.fragment\u映射,null,false);
FragmentTransaction ft=getFragmentManager().beginTransaction();
mMap=((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
SupportMapFragment fmap=(SupportMapFragment)getFragmentManager().findFragmentById(R.id.map);
如果(fmap==null){
fmap=SupportMapFragment.newInstance();
ft.add(R.id.map,fmap);
}
ft.commit();
manager=(LocationManager)getActivity().getSystemService(Context.LOCATION\u服务);
标准=新标准();
标准.设定准确度(标准.准确度/精细度);
标准。setAltitudeRequired(false);
标准。需要设置(假);
条件.setCostAllowed(true);
标准.设置电源要求(标准.电源介质);
字符串提供程序=manager.getBestProvider(条件为true);
requestLocationUpdates(提供者,0,0,locationListener);
loca=manager.getLastKnownLocation(提供者);
如果(loca!=null){
CameraPosition CameraPosition=新建CameraPosition.Builder()
.target(新LatLng(loca.getLatitude(),loca.getLongitude())//将地图中心设置为用户位置
.zoom(16)//设置缩放
.tilt(40)//将相机的倾斜设置为30度
.build();//从生成器创建CameraPosition
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
如果(!manager.isProviderEnabled(LocationManager.GPS\U提供程序)){
buildAlertMessageNoGps();
}
返回rootView;
}
私有最终位置Listener LocationListener=新位置Listener(){
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
//TODO自动生成的方法存根
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
//TODO自动生成的方法存根
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
}
@凌驾
已更改位置上的公共无效(位置){
插入标记(位置);
}
};
/**
*在这里,我们可以添加标记或线条、添加侦听器或移动摄影机
*在非洲附近加一个标记。
*
*当我们确定{@link#mMap}不是null时,应该只调用一次。
*/
私有void setUpMap(){
mMap.setMyLocationEnabled(真);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
}
公共空白插入标记(位置){
如果(位置!=null){
双纬度=location.getLatitude();
double lng=location.getLongitude();
lati=字符串的值(lat);
longi=管柱的数值(lng);
mMap.addMarker(新标记选项().position(新板条(板条,板条)).title(板条+“,”+longi));
}否则{
lati=“Latitudine non-trovata”;
longi=“Longitudine non-trovata”;
}
}
私有void buildAlertMessageNoGps(){
final AlertDialog.Builder=新建AlertDialog.Builder(getActivity());
builder.setMessage(“Il tuo GPSèdisabilitato,vuoi attivarlo?”)
.setCancelable(错误)
.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
public void onClick(@SuppressWarnings(“未使用”)最终对话框接口对话框,@SuppressWarnings(“未使用”)最终int-id){
startActivity(新的意图(android.provider.Settings.ACTION\u LOCATION\u SOURCE\u Settings));
}
})
.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
public void onClick(final DialogInterface dialog,@SuppressWarnings(“未使用”)final int-id){
getActivity().finish();
}
});
final AlertDialog alert=builder.create();
alert.show();
}
}
非常感谢:)您可能想将该行更改为
mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
或
因为您应该在活动中调用getFragmentManager()
mMap =((SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();