android.view.InflateException:二进制 XML 文件行 #1:膨胀 class 片段 GOOGLE 地图时出错

android.view.InflateException: Binary XML file line #1: Error inflating class fragment GOOGLE MAPS

我只是使用 Google 地图 activity 模板创建了一个 activity 并放置了我的 API 键,当我 运行 它时,我收到了这个错误

(android.view.InflateException: Binary XML file line #1: Error inflating class fragment ).

这是我的 xml 文件:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto" 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" />

这是我的 activity:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}
    @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));
}
}

几天前我遇到了类似的问题。我遇到这个问题是因为我升级了我的 android SDK 并且目标 sdk 是 23.
Google 更改了 sdk 和 Fragment 周围的一些东西。当您查看 xml 文件时,您会发现问题出在第 1 行 - 可能是 <fragment> 属性。
好的,这里是答案:试试看:
实现接口 OnMalReadyCallback:

public class WorkPlaceRegisterActivity extends FragmentActivity implements OnMapReadyCallback{

在 onCreate() 中使用这个:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_work_map_form_registration);

    **SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.workMapRegistration);
    mapFragment.getMapAsync(this);**

接下来添加onMapReady重写方法:

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

我希望我没有遗漏任何东西,我稍后会检查希望得到帮助。

在 activity xml:

中尝试秘密片段
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">

<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/workMapRegistration"
    tools:context="com.mytway.activity.registerformactivity.WorkPlaceRegisterActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    tools:layout="@layout/abc_action_bar_title_item" />



</RelativeLayout>

我在 build.gradle 文件中的依赖项:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/Parse-1.10.3/Parse-1.10.3.jar')
compile files('libs/Parse-1.10.3/bolts-android-1.2.1.jar')
compile files('libs/Parse-1.10.3/ParseCrashReporting-1.10.3.jar')
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.android.support:appcompat-v7:23.1.0'
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
compile 'com.google.android.gms:play-services:8.4.0'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'

}