我在Android中使用Google Maps v2,但为什么会出现异常错误?

时间:2013-04-11 12:45:20

标签: android map

我没有显示任何针脚;我只是想展示一张地图,但是当我试图开始一个活动时,它会被强制关闭并给出异常:

  

显示某种类型的片段类错误或二进制错误

清单文件是:

  

Mani.xml

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

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

    <permission
          android:name="in.wptrafficanalyzer.LocationGoogleMapV2Demo.permission.MAPS_RECEIVE"
          android:protectionLevel="signature"/>

    <uses-permission android:name="in.wptrafficanalyzer.LocationGoogleMapV2Demo.permission.MAPS_RECEIVE"/>

    <uses-permission android:name="android.permission.INTERNET"/>
    <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-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="in.wptrafficanalyzer.locationgooglemapv2demo.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>

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyAdupvz6Hpet0O5OxfAg8n-OrrFPK8_JZ4"/>
    </application>
</manifest>

异常

04-11 18:07:42.476: E/AndroidRuntime(13119): FATAL EXCEPTION: main
04-11 18:07:42.476: E/AndroidRuntime(13119): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.locationgooglemapv2demo/in.wptrafficanalyzer.locationgooglemapv2demo.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.os.Looper.loop(Looper.java:137)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.app.ActivityThread.main(ActivityThread.java:4895)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at java.lang.reflect.Method.invokeNative(Native Method)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at java.lang.reflect.Method.invoke(Method.java:511)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at dalvik.system.NativeStart.main(Native Method)
04-11 18:07:42.476: E/AndroidRuntime(13119): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:306)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.app.Activity.setContentView(Activity.java:1912)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at in.wptrafficanalyzer.locationgooglemapv2demo.MainActivity.onCreate(MainActivity.java:12)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.app.Activity.performCreate(Activity.java:5163)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
04-11 18:07:42.476: E/AndroidRuntime(13119):     ... 11 more
04-11 18:07:42.476: E/AndroidRuntime(13119): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
04-11 18:07:42.476: E/AndroidRuntime(13119):     ... 21 more
04-11 18:07:42.476: E/AndroidRuntime(13119): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
04-11 18:07:42.476: E/AndroidRuntime(13119):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
04-11 18:07:42.476: E/AndroidRuntime(13119):     at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
04-11 18:07:42.476: E/AndroidRuntime(13119):     ... 24 more

3 个答案:

答案 0 :(得分:1)

你的主要节目显示minSDK = 8,这意味着你必须使用SupportMapFragment而不是Fragment:

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

请参阅下面的我的博文。我试图用Google Maps API V2 for Android构建应用程序所需的内容。

  1. Solution to Problems with Maps API V2(using google_play_services_lib).
  2. Creating an Android App with Maps API V2 for minSDK=8 (Android 2.2 Froyo).
  3. Creating an Android App with Maps API V2 for minSDK=11 (Android 3.0 HoneyComb).

答案 1 :(得分:0)

要将SupportMapFragment添加到项目中,您需要添加google-support-v4库和Google地图库。

检查堆栈溢出问题 Google Maps Fragment doesn't load

答案 2 :(得分:0)

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

在清单文件中添加这些行