【问题标题】:Android Google Map - Error inflating class fragmentAndroid Google Map - 膨胀类片段时出错
【发布时间】:2015-06-21 04:34:53
【问题描述】:

我想在我的Activity 上显示GoogleMap。我已经完成了所有必要的步骤,包括密钥生成、链接google-play-services_lib、添加android-support-v4.jar。但是由于Fragment,我面临一个错误。我尝试了很多解决方案,花了很多时间但无法解决这个问题。请帮我。

MapActivity.java

package com.example.classified_android_app;

import android.os.Bundle;   
import android.support.v4.app.FragmentActivity;

public class MapActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
    }
}

ma​​p.xml

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

    <fragment
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"
        map:cameraTargetLat="17.660324"
        map:cameraTargetLng="75.906601"
        map:cameraZoom="15" >
    </fragment>

</RelativeLayout>

清单

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

    <uses-sdk
        android:maxSdkVersion="19"
        android:minSdkVersion="16"
        android:targetSdkVersion="19" />

    <permission
        android:name="com.example.classified_android_app.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.example.classified_android_app.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <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" >
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="my key" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".MapActivity"
            android:label="Google Map"
            android:theme="@style/MyTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

日志

06-21 09:54:08.592: W/dalvikvm(13704): threadid=1: thread exiting with uncaught exception (group=0x4191d908)
06-21 09:54:08.630: E/AndroidRuntime(13704): FATAL EXCEPTION: main
06-21 09:54:08.630: E/AndroidRuntime(13704): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.classified_android_app/com.example.classified_android_app.MapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
06-21 09:54:08.630: E/AndroidRuntime(13704):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at android.os.Looper.loop(Looper.java:153)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at android.app.ActivityThread.main(ActivityThread.java:4987)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at java.lang.reflect.Method.invokeNative(Native Method)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at java.lang.reflect.Method.invoke(Method.java:511)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at dalvik.system.NativeStart.main(Native Method)
06-21 09:54:08.630: E/AndroidRuntime(13704): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
06-21 09:54:08.630: E/AndroidRuntime(13704):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-21 09:54:08.630: E/AndroidRuntime(13704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)

【问题讨论】:

  • 你在使用 Eclipse 吗?
  • 你的android:minSdkVersion="16",所以你应该使用MapFragment

标签: java android android-fragments android-fragmentactivity google-maps-api-2


【解决方案1】:

主要问题是你的android:minSdkVersion="16"

原始文档所说的是

如果您的 android:minSdkVersion &gt; 12 则应使用 MapFragment 扩展 Activity

还有

如果你的android:minSdkVersion &lt; 12 那么你应该使用SupportMapFragment 扩展FragmentActivity

【讨论】:

    【解决方案2】:

    尝试替换

    <fragment
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment"
            map:cameraTargetLat="17.660324"
            map:cameraTargetLng="75.906601"
            map:cameraZoom="15" >
        </fragment>
    

    <fragment
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
             android:name="com.google.android.gms.maps.SupportMapFragment"
            map:cameraTargetLat="17.660324"
            map:cameraTargetLng="75.906601"
            map:cameraZoom="15" >
        </fragment>
    

    见android官网How to add static fragment from XML提供的这个链接

    【讨论】:

    • java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.classified_android_app/com.example.classified_android_app.MapActivity}:android.view.InflateException:二进制 XML 文件第 6 行:膨胀错误类片段
    【解决方案3】:

    尝试将此添加到您的 onCreate() 中。

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

    并替换它。

    <fragment
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment"
            map:cameraTargetLat="17.660324"
            map:cameraTargetLng="75.906601"
            map:cameraZoom="15" >
        </fragment>
    

    <fragment
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />
            map:cameraTargetLat="17.660324"
            map:cameraTargetLng="75.906601"
            map:cameraZoom="15" >
        </fragment>
    

    【讨论】:

    • 当我在片段中添加 xmlns:android="schemas.android.com/apk/res/android" 时,它会显示此错误 - 为标签片段找到意外的命名空间前缀“xmlns”
    • 只要将 SupportMapFragment 添加到 onCreate() 中,就应该按照您的方式工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多