android.view.InflateException:二进制XML文件行#13..While使用MapFragment

时间:2017-04-24 16:56:40

标签: 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: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的一部分。我根据文档做了一切,但是当我运行时,应用程序崩溃并出现以下错误

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文件是问题吗?或者我做错了什么?

3 个答案:

答案 0 :(得分:1)

As you are using SupportMapFragment from Fragment its best prectice ot Use com.google.android.gms.maps.MapView instead of SupportMapFragment

<com.google.android.gms.maps.MapView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:id="@+id/map" />

Your MapFragment class as below:

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();
    }

}

Hope this will help~

答案 1 :(得分:0)

我有同样的问题!尝试在布局中添加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" />

答案 2 :(得分:0)

看起来你已经混淆了the official documentation,并试图在片段中放置片段。

我也看到你没有给你的MapFragment课程膨胀......所以,错误似乎存在于其他地方。

class="com.google.android.gms.maps.SupportMapFragment"

引用文档......

  

默认情况下,定义应用程序布局的XML文件位于   res/layout/activity_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" />
     

地图活动Java文件

     

默认情况下,将命名定义maps活动的Java文件   MapsActivity.java。它应该包含以下代码   包名称:

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课程,请先从

开始
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 --->