【问题标题】:android.view.InflateException: Binary XML file line #1: Error inflating class fragment GOOGLE MAPS [closed]android.view.InflateException:二进制 XML 文件第 1 行:膨胀类片段 GOOGLE MAPS 时出错 [关闭]
【发布时间】:2016-05-19 10:58:30
【问题描述】:

我只是使用谷歌地图活动模板创建了一个活动并输入了我的 API 密钥,当我运行它时,我收到了这个错误

(android.view.InflateException: 二进制 XML 文件第 1 行:错误 膨胀类片段)。

这是我的 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" />

这是我的活动:

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

【问题讨论】:

  • 提供你的活动java代码和xml代码。
  • 提供您的活动代码。!!
  • xml 请,我认为在那种情况下,这是最重要的......
  • 请不要搁置它。我需要回答,因为在更新我的 sdk 和 google play 服务之前它工作正常,现在不行了。

标签: android google-maps android-fragments android-studio google-maps-api-2


【解决方案1】:

几天前我遇到了类似的问题。我遇到了这个问题,因为我升级了我的 android SDK 并且目标 sdk 是 23。
谷歌改变了 sdk 和 Fragment 周围的东西。当您查看 xml 文件时,您会发现问题出在第 1 行 - 可能是 &lt;fragment&gt; 属性。
好的,这里有一个答案:试试看:
实现接口 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'

}

【讨论】:

  • 它都是自动生成的代码,和你的一样。是的,我在更新我的 sdk 和 google play 服务后遇到了这个错误。
  • 哦,你是对的,尝试在xml中隐藏片段:
  • 我也尝试了秘密片段但仍然无法正常工作。我不知道这个新更新有什么问题。
  • 嗨 Muhammad,这是一个在我的 github 上使用谷歌地图的活动:github.com/BurmerLab/RegisterFormAndroid/blob/master/app/src/… 我还在那里添加了新的(android marshmallow)地理定位权限,并在点击后添加了一个标记。也许会有所帮助。此活动适用于我的电脑和我的 htc 电脑 :​​) 祝你好运
  • 谢谢 hetmanJIIIS 我要去试试..
猜你喜欢
  • 2016-11-16
  • 2014-05-21
  • 2013-11-21
  • 2022-01-17
  • 2014-08-13
  • 2015-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多