Java android.view.InflateException:使用MapFragment时的二进制XML文件行#13
我想在我的android应用程序中加入地图,所以我按照官方文件的要求,最终得到了这些文件 fragment_map.xml:Java android.view.InflateException:使用MapFragment时的二进制XML文件行#13,java,android,xml,google-maps-android-api-2,Java,Android,Xml,Google Maps Android Api 2,我想在我的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:a
<?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>
正如您可能已经从我的代码中看到的。我试图在片段中显示地图,而片段又作为查看页面的一部分显示。我按照文档做了所有事情,但是当我运行应用程序时,它由于以下错误而崩溃
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_版本的元标记
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
看起来您混淆了,并试图在片段中放置片段
我还看到你没有夸大你的MapFragment
类。。。因此,错误似乎存在于其他地方
class="com.google.android.gms.maps.SupportMapFragment"
引用文档
默认情况下,定义应用程序布局的XML文件位于
res/layout/activity\u maps.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="com.google.android.gms.maps.SupportMapFragment" />
所以,问题中显示的所有代码都应该在活动中,而不是片段中,然后替换
getActivity().getSupportFragmentManager().findFragmentById(R.id.map)
简单地
getSupportFragmentManager().findFragmentById(R.id.map)
如果您确实想扩展SupportMapFragment
类,请从下面开始
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 --->
当您使用SupportMapFragment
中的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" />
希望这将有助于~添加日志的其余部分InflateException
不应该是唯一的错误。此外,您应该真正修复MapFragment extends Fragment
以扩展SupportMapFragment
。。。。在这里没有理由将片段放入片段android:name
与class
冲突。你应该使用一个片段,而不是一个活动。我在发布我的答案,没有重复使用他的代码。我只是说android:name=“com.testing.shivam.main活动”
可能会使应用程序崩溃…它对我来说非常有效,因此我认为这些信息对某些人会有帮助。我对此有一些澄清。默认情况下,当我打开地图时,它处于缩小状态。我应该做哪些更改,以便放大用户的当前位置?我已经更新了答案。试试这个:CameraUpdate CameraUpdate=CameraUpdateFactory.newLatLngZoom(newlatlng(13.1,-37.9),10);map.animateCamera(cameraUpdate);//根据您的位置更改latlong谢谢。我试试这个
<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 --->
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
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();
}
}