我想要显示的地图有一个奇怪的问题。有时崩溃,下一个错误:“二进制XML文件行#8:二进制XML文件行#8:错误膨胀类片段”有时它显示带有Google徽标的灰色背景(如谷歌地图的背景)。 我已经搜索并尝试了很多来解决问题,但没有成功。 这是代码:
public class MapFragment extends Fragment implements OnMapReadyCallback{
private GoogleMap mMap;
public MapFragment() {
// Required empty public constructor
}
public static MapFragment newInstance() {
MapFragment fragment = new MapFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_map, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.mMap = googleMap;
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
}
public interface OnMapFragmentInteractionListener {
// TODO: Update argument type and name
void onMapFragmentInteraction(Uri uri);
}
public void updateMap(String latitude, String longitude){
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(latitude),Double.parseDouble(longitude)),15));
}
}
和xml文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context="com.andrei.myapp.MapFragment">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
答案 0 :(得分:0)
当您使用SupportMapFragment
Fragment
时,com.google.android.gms.maps.MapView
使用SupportMapFragment
代替<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
MapFragment
您的public class MapFragment extends Fragment {
MapView mapView;
GoogleMap mMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
mapView = (MapView) view.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
mMap = mapView.getMap();
mMap.getUiSettings().setMyLocationButtonEnabled(false);
mMap.setMyLocationEnabled(true);
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(13.1, -37.9), 10);
mMap.animateCamera(cameraUpdate);
return view;
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
课程如下:
set list
set lischars=space:.
希望这会有所帮助〜