【问题标题】:android: Binary XML file line #9: error inflating class fragmentandroid: Binary XML file line #9: error inflating class fragment
【发布时间】:2014-01-13 04:06:11
【问题描述】:

我试图在 android 应用程序中显示地图。我正在使用地图 V2。但是会抛出错误“Binary XML file line #9: error inflating class fragment”。应用在启动时崩溃。

下面是我的代码。请帮我解决这个问题

MainActivity.Java

    package com.example.googlemapsv2;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;

    public class MainActivity extends FragmentActivity {

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

activity_main.xml

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

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

Android 清单

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemapsv2"
android:versionCode="1"
android:versionName="1.0" >
 <permission
    android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.googlemapsv2.permission.MAPS_RECEIVE" />
<uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.googlemapsv2.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<meta-data
 android:name="com.google.android.maps.v2.API_KEY"
 android:value="AIzaSyBAvQK4RAin10dqyflr95tH45Ce_Eko3G0" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</manifest>

【问题讨论】:

  • 将此android:minSdkVersion="12" 更改为11
  • @Raghunandan:这对我没有任何帮助。我正在使用模拟器 4.3。为了你,我试过了。仍然有同样的错误
  • developers.google.com/maps/documentation/android/reference/com/…。仅当您针对 API 12 及更高版本时,请检查此使用此类。否则,使用 SupportMapFragment .. 为什么没有帮助?您的 min sdk 为 12,在这种情况下您应该使用 MapFragment 并扩展 Activity。我也将其发布为评论而不是答案。需要更多信息。您是否引用了 google play services library 项目。
  • 是的.. 我引用了谷歌播放服务。我希望 minSDKVersion 与 TargetSDKVersion 不同。根据您的评论,我将 MapFragment 与 MinSDKVersion 12 一起使用。正如您所提到的,我通过针对 API 级别 12 及更高级别来使用它。无需将其更改为 11。
  • 那么你需要在xml中将SupportMapFragment更改为MapFragment并且你的Activity类应该扩展Activity。还发布堆栈跟踪

标签: android api map


【解决方案1】:

我遇到了同样的问题,我通过添加官方文档中指定的权限解决了它(Google map doc on permissions)

<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

【讨论】:

    【解决方案2】:

    根据我以前的经验,API 18/19 必须添加清单 -> 应用程序元数据 Q 更新随版本更新:

    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

    【讨论】:

      【解决方案3】:

      以下必须在清单文件的application标签内

      参考

      https://developers.google.com/maps/documentation/android/start#add_the_google_play_services_version_to_your_apps_manifest

      <meta-data
       android:name="com.google.android.maps.v2.API_KEY"
       android:value="AIzaSyBAvQK4RAin10dqyflr95tH45Ce_Eko3G0" />
      <meta-data
      android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version" />
      

      因为你的 minsdk 是 12。你需要使用 MapFragment 并且你的类应该扩展 Activity

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

      然后

      public class MainActivity extends Activity
      

      确保您正确引用了库项目并在 google api 控制台中为 android 启用了地图。

      【讨论】:

      • 我可以 gt 这个 .. 谢谢 Raghu .. 问题解决了 .. .
      • 我遇到了同样的问题。感谢您的回答!
      【解决方案4】:

      试试 SupportMapFragment activity_main.xml

      android:name 更改为简单类

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

      MainActivity.java

      public class MainActivity extends FragmentActivity {
      
          // Google Map
          private GoogleMap googleMap;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              try {
                  // Loading map
                  initializeMap();
      
              } catch (Exception e) {
                  e.printStackTrace();
              }
      
          }
      
          /**
           * function to load map. If map is not created it will create it for you
           * */
          private void initializeMap() {
              if (googleMap == null) {
                  googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
                  // check if map is created successfully or not
                  if (googleMap == null) {
                      Toast.makeText(getApplicationContext(),
                          "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                          .show();
                  }
              }
          }
      
          @Override
          protected void onResume() {
              super.onResume();
          }
      }
      

      更改 SDK 版本

      <uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="17" />
      

      Raghunandan 是对的,你应该&lt;application&gt;. 内写&lt;meta&gt;tag 代码

      【讨论】:

      • 应用在 GoogleMap 对象初始化之前就崩溃了
      • 请参考developers.google.com/maps/documentation/android/…。请检查主题添加地图。检查步骤 1,2 和 3。所以你应该看到地图。如果要放置标记,则需要初始化地图对象。
      • 应用在此之前就崩溃了。 将 android:name 更改为简单类会有什么不同?再次参考文档底部的文档步骤 1
      • @Raghunandan 是的,这是真的,我用android:name="com.google.android.gms.maps.SupportMapFragment" 进行了测试,这也有效,但我已经按照this 实施。
      猜你喜欢
      • 1970-01-01
      • 2020-06-27
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      相关资源
      最近更新 更多