我试图在 fragment 中使用谷歌地图 api,但它总是返回 null。我无法处理它,任何帮助都会得到帮助。有代码;
public class Map extends android.app.Fragment implements OnMapReadyCallback {
private MapView mMapView;
private GoogleMap mGoogleMap;
String response_vehicles;
Location location;
float map_zoom=14;
public Map() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mapView = inflater.inflate(R.layout.fragment_map, null, false);
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return mapView;
}
@Override
public void onMapReady(GoogleMap map) {
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LatLng sydney = new LatLng(-33.867, 151.206);
map.setMyLocationEnabled(true);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
map.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
}
并且有xml文件;
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context=".Fragments.Map"
android:name="com.google.android.gms.maps.SupportMapFragment" />
我在mapFragment.getMapAsync(this);处给出错误NullPointerException 我该如何解决这个问题?我认为 list 文件和权限是完全正确的。
最佳答案
这是在 fragment 类中使用 map 的解决方案
fragment 类
public class DemoMapFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap googleMap;
private View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.activity_demo, container, false);
// Try to obtain the map from the SupportMapFragment.
SupportMapFragment mapFragment = SupportMapFragment.newInstance();
getChildFragmentManager().beginTransaction().replace(R.id.map, mapFragment).commit();
mapFragment.getMapAsync(this);
return rootView;
}
@Override
public void onMapReady(final GoogleMap googleMap) {
this.googleMap = googleMap;
}
}
还有你的 xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_demo"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
关于java - 如何在android中的fragment中使用谷歌地图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38305934/