android.view.InflateException:二进制 XML 文件行 #13.. 使用 MapFragment 时
android.view.InflateException: Binary XML file line #13..While using MapFragment
我想在我的 android 应用程序中包含地图,所以我遵循了官方文档并最终得到了这些文件
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));
}
}
正如您从我的代码中看到的那样。我试图在片段中显示地图,片段又显示为 ViewPager
的一部分。我按照文档做了所有事情,但是当我 运行 it
时应用程序崩溃并出现以下错误
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 语句引起的。由于我是这个地图的新手,所以我找不到任何错误。
XML 文件是问题所在吗?还是我做错了什么?
我遇到了同样的问题!尝试在布局中添加 android:name="com.testing.shivam.MainActivity" 到 "fragment" !它解决了我的问题
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:name="com.testing.shivam.MainActivity"/>
如果这对您不起作用,请检查您是否已在应用程序标签下的清单文件中为 google_play_services_version 正确添加元标签,
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
看起来您混淆了 the official documentation 并试图将片段放在片段中。
我也看到你没有夸大你的 MapFragment
class... 所以,错误似乎存在于其他地方。
class="com.google.android.gms.maps.SupportMapFragment"
正在引用文档...
By default, the XML file that defines the app's layout is at
res/layout/activity_maps.xml
. It contains the following code:
<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=".MapsActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
The maps activity Java file
By default, the Java file that defines the maps activity is named
MapsActivity.java
. It should contain the following code after your
package name:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
因此,您问题中显示的所有代码实际上应该在 Activity 中,而不是片段中,然后替换
getActivity().getSupportFragmentManager().findFragmentById(R.id.map)
只需
getSupportFragmentManager().findFragmentById(R.id.map)
如果你真的想扩展 SupportMapFragment
class,从这个开始
public class MapFragment extends SupportMapFragment
implements OnMapReadyCallback {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getMapAsync(this);
}
并将其加载到 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=".MapsActivity"
android:name="YOUR.PACKAGE.NAME.MapFragment" /> <!--- See here --->
当您使用 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 class 如下:
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();
}
}
希望对你有所帮助~
我想在我的 android 应用程序中包含地图,所以我遵循了官方文档并最终得到了这些文件
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));
}
}
正如您从我的代码中看到的那样。我试图在片段中显示地图,片段又显示为 ViewPager
的一部分。我按照文档做了所有事情,但是当我 运行 it
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 语句引起的。由于我是这个地图的新手,所以我找不到任何错误。
XML 文件是问题所在吗?还是我做错了什么?
我遇到了同样的问题!尝试在布局中添加 android:name="com.testing.shivam.MainActivity" 到 "fragment" !它解决了我的问题
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"
android:name="com.testing.shivam.MainActivity"/>
如果这对您不起作用,请检查您是否已在应用程序标签下的清单文件中为 google_play_services_version 正确添加元标签,
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
看起来您混淆了 the official documentation 并试图将片段放在片段中。
我也看到你没有夸大你的 MapFragment
class... 所以,错误似乎存在于其他地方。
class="com.google.android.gms.maps.SupportMapFragment"
正在引用文档...
By default, the XML file that defines the app's layout is at
res/layout/activity_maps.xml
. It contains the following code:<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=".MapsActivity" android:name="com.google.android.gms.maps.SupportMapFragment" />
The maps activity Java file
By default, the Java file that defines the maps activity is named
MapsActivity.java
. It should contain the following code after your package name:public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap;
因此,您问题中显示的所有代码实际上应该在 Activity 中,而不是片段中,然后替换
getActivity().getSupportFragmentManager().findFragmentById(R.id.map)
只需
getSupportFragmentManager().findFragmentById(R.id.map)
如果你真的想扩展 SupportMapFragment
class,从这个开始
public class MapFragment extends SupportMapFragment
implements OnMapReadyCallback {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getMapAsync(this);
}
并将其加载到 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=".MapsActivity"
android:name="YOUR.PACKAGE.NAME.MapFragment" /> <!--- See here --->
当您使用 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 class 如下:
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();
}
}
希望对你有所帮助~