Google会在Fragment中映射SupportMapFragment错误

时间:2017-03-17 11:01:59

标签: android google-maps

旧版应用使用MapView。我更改了SupportMapFragment错误。我知道如何解决这个问题。

  

流程:com.test.app,PID:7847
  android.view.InflateException:二进制XML文件行#14:错误   膨胀类片段

XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

</FrameLayout>

片段

public class MyFragment extends Fragment implements OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_map, null);

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

    buildGoogleApiClient();


    return rootView;
}

的build.gradle

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.android.gms:play-services-location:10.2.0'
    compile 'com.google.android.gms:play-services-maps:10.2.0'
    compile 'se.emilsjolander:stickylistheaders:2.5.2'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.facebook.android:facebook-android-sdk:4.0.0'
//compile 'com.google.firebase:firebase-messaging:10.2.0'


 }

3 个答案:

答案 0 :(得分:1)

CountableClosedRange

}

public class MapFragment extends Fragment implements OnMapReadyCallback {

SupportMapFragment mapFragment;

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

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

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

答案 1 :(得分:1)

这就是我设置的方式

<强> XML

 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_marginTop="5dp"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/mapView"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        tools:context=".MainActivity" />

代码:

import android.support.v4.app.Fragment;


SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager()
                .findFragmentById(R.id.mapView);
        mapFragment.getMapAsync(this);

让您fragment实施OnMapReadyCallback(例如extends Fragment implements OnMapReadyCallback)并复制此方法:

@Override
    public void onMapReady(GoogleMap map) {
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.setTrafficEnabled(false);
        map.setIndoorEnabled(false);
        map.setBuildingsEnabled(false);
        map.getUiSettings().setZoomControlsEnabled(true);
        LatLng wotw = new LatLng(getLat(), getLong());
        map.addMarker(new MarkerOptions().position(wotw)
                .title("Walk of the Week"));
        map.moveCamera(CameraUpdateFactory.newLatLng(wotw));
        map.animateCamera(CameraUpdateFactory.zoomTo(15.0f));
    }

确保您的{8}中也有API密钥:

Manifest

和相应的权限:

<meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="API_KEY_HERE"/>

答案 2 :(得分:1)

我以这种方式解决了这个问题。

if (view != null) {
   ViewGroup parent = (ViewGroup) view.getParent();
   if (parent != null)
      parent.removeView(view);
}

try {
     view = inflater.inflate(R.layout.fragment_partners_map, container, false);
} catch (InflateException e) {
}

我第一次检查是否存在Fragments父视图,然后重新创建父节点,否则我创建它。