二进制XML文件行#1:错误膨胀类片段MapFragment

时间:2016-06-22 05:32:29

标签: android google-maps android-fragments supportmapfragment mapfragment

我有一个带有一些碎片的导航抽屉。每个片段在布局中都有一个MapFragment。最初,当应用程序运行导航抽屉时,单击打开片段一次,但是当我单击相同的导航项以第二次打开相同的片段时(在访问其他片段后),我在select count(course) as pcourse, course from studies group by course order by pcourse dec; 中收到以下错误

onCreateView()

以下是我的片段代码:

user_map_Fragment.xml

Binary XML file line #1: Error inflating class fragment 

MainFragment.cs

 <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:id="@+id/parentContainer"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:name="com.google.android.gms.maps.MapFragment"/>

......
</LinearLayout>

崩溃日志:

public class MainFragement : Android.Support.V4.App.Fragment, IOnMapReadyCallback, ILocationListener, Android.Views.View.IOnKeyListener
    {

        private GoogleMap mMap;

        private LocationManager _locationManager;
        private string _locationProvider;
        private Android.Locations.Location _currentLocation;

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            if (v == null)
            {
                v = inflater.Inflate(Resource.Layout.Main, container, false);

                SetUpMap();
                InitializeLocationManager();

            }
            return v;
        }
}



private void SetUpMap()
        {
            if (mMap == null)
            {
                Activity.FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this);
            }
        }

我该如何解决这个问题?

4 个答案:

答案 0 :(得分:8)

而不是这个

 <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:name="com.google.android.gms.maps.MapFragment"/> 

使用此

 <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="200dp"
             />

在java类

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class ClinicFragment extends Fragment implements OnMapReadyCallback {

    private GoogleMap mMap;

    public static ClinicFragment newInstance() {
        ClinicFragment fragment = new ClinicFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_maps, null, false);

        SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        return view;
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

答案 1 :(得分:1)

尝试更改

android:name="com.google.android.gms.maps.SupportMapFragment"
//and
Activity.FragmentManager.FindFragmentById<SupportMapFragment>(Resource.Id.map).GetMapAsync(this);

而不是

android:name="com.google.android.gms.maps.MapFragment"
//and
Activity.FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this);

编辑1

删除你的android.app.Fragment并导入支持包

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

编辑2

试试这个

SupportMapFragment m = ((SupportMapFragment) getChildFragmentManager()
    .findFragmentById(R.id.map));

这可能有助于你

答案 2 :(得分:0)

android:hardwareAccelerated="true"发送到MapsActivity上的AndroidManifest.xml

为我工作。

答案 3 :(得分:0)

就我而言,即使使用SupportMapFragment,我也忘记在Google地图中添加API_KEY。

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="200dp" />

清单中

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/google_maps_key" />