安卓:二进制 XML 文件第 9 行:解析 fragment 类时出错。

6
我试图在安卓应用中显示地图。我使用的是地图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
@Raghunandan:那不会对我有任何帮助。我正在使用模拟器4.3版本。为了您的利益,我尝试过了。仍然遇到相同的错误。 - ARN
1
请查看此链接:https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment。仅在目标API为12及以上时使用此类。否则,请使用SupportMapFragment。为什么它不能帮助你?如果您的min sdk是12,则应使用MapFragment并扩展Activity。我将其发布为评论而不是答案。需要更多信息。您是否已引用Google Play服务库项目? - Raghunandan
1
是的..我引用了Google Play服务。希望minSDKVersion与TargetSDKVersion不同。根据您的评论,我使用MinSDKVersion 12的MapFragment。如您所述,我正在针对API级别12及以上使用它。没有必要将其更改为11。 - ARN
1
然后您需要在xml中将SupportMapFragment更改为MapFragment,并且您的Activity类应该扩展Activity。同时,请发布堆栈跟踪。 - Raghunandan
不是针对此问题的特定解决方案,但相关并解决了我的问题。清单不能同时指定 com.google.android.maps.v2.API_KEY(已弃用)和 com.google.android.geo.API_KEY。如果您的清单两者都有,请删除 maps.v2 的那个并保留 geo 的那个。如果两者都被指定,则会遇到这个膨胀错误问题。具体请参见 https://developers.google.com/maps/documentation/android/start。 - JZC
4个回答

9
以下内容必须放在清单文件的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地图。

2
我可以搞定这个..谢谢Raghu..问题解决了.. - ARN
我也遇到了同样的问题。感谢您的答案! - Bonton255

5

尝试在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" />

拉古南丹是正确的,你应该在<application>标签内写入<meta>标签代码。

应用程序在GoogleMap对象初始化之前就崩溃了。 - Raghunandan
应用程序甚至在此之前崩溃。将android:name更改为简单的类,会有什么不同?再次参考文档步骤1,文档底部。 - Raghunandan
“android:name” 属性在 <fragment> 标签中指定了要在布局中实例化的“Fragment 类”。 - Raghunandan
@Raghunandan 是的,没错,但是这里他使用了 Support Fragment,请参见 https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/SupportMapFragment - Chintan Khetiya
1
@Raghunandan 是的,没错,我已经使用了 android:name="com.google.android.gms.maps.SupportMapFragment" 进行测试,并且也运行良好,但是我是按照这个来实现的。 - Chintan Khetiya
显示剩余4条评论

1
我遇到了同样的问题,我按照官方文档中指定的方式添加了权限来解决它(Google地图关于权限的文档<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"/>

1
在我以往的经验中,API 18/19必须添加到清单文件中 -> 应用程序元数据Q更新版本如下: <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接