我想在我的 Android 应用程序中包含 map ,因此我遵循官方文档并最终得到了这些文件
fragment_map.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the map fragment"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
tools:context="com.example.nirmal.attendancetracker"
class="com.google.android.gms.maps.SupportMapFragment"
/>
</LinearLayout>
MapsFragment.java:
public class MapFragment extends Fragment implements OnMapReadyCallback{
SupportMapFragment mapFragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View subView = inflater.inflate(R.layout.fragment_map,container,false);
mapFragment = (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return subView;
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
正如您可能从我的代码中看到的那样。我试图在 fragment 中显示 map ,该 fragment 又显示为 ViewPager 的一部分。我按照文档做了所有事情,但是当我运行它时,应用程序崩溃并出现以下错误
FATAL EXCEPTION: main Process: com.example.nirmal.attendancetracker, PID: 31903
android.view.InflateException: Binary XML file line #13: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.example.nirmal.attendancetracker.MapFragment.onCreateView(MapFragment.java:27)
我以前见过这些错误。它们通常是由错误的 XML 语句引起的。由于我对这个 map 很陌生,所以我找不到任何错误。
XML 文件有问题吗?或者我做错了什么?
最佳答案
当您使用 Fragment
中的 SupportMapFragment
时,最好使用 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 map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View subView = inflater.inflate(R.layout.fragment_map, container, false);
mapView = (MapView) subView.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
try {
MapsInitializer.initialize(this.getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(13.1, -37.9), 10);
map.animateCamera(cameraUpdate);
return subView;
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
希望对你有帮助~
关于java - android.view.InflateException : Binary XML file line #13.。使用MapFragment时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43593831/