运行Google maps API V2示例时出错

时间:2015-02-23 18:24:07

标签: android eclipse google-maps google-maps-android-api-2

我正在尝试这里的例子https://developers.google.com/maps/documentation/android/start

我在Eclipse中工作,代码中没有显示错误。我已将google-play-services-lib链接起来,并将项目构建目标设置为最新版本的Google API。我输入了我的API密钥,并在控制台的服务菜单中打开了谷歌地图android api v2。有什么想法我收到这个错误?我正在测试一个真实的设备,一个运行棒棒糖的nexus 5(5.0.1)

我的logcat在

之下
FATAL EXCEPTION: main
02-23 17:47:48.927: E/AndroidRuntime(8735): Process: com.juliette.basicgooglemap, PID: 8735
02-23 17:47:48.927: E/AndroidRuntime(8735): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.juliette.basicgooglemap/com.juliette.basicgooglemap.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.os.Looper.loop(Looper.java:135)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.ActivityThread.main(ActivityThread.java:5221)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at java.lang.reflect.Method.invoke(Native Method)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at java.lang.reflect.Method.invoke(Method.java:372)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-23 17:47:48.927: E/AndroidRuntime(8735): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.Activity.setContentView(Activity.java:2144)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at com.juliette.basicgooglemap.MainActivity.onCreate(MainActivity.java:11)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.Activity.performCreate(Activity.java:5933)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-23 17:47:48.927: E/AndroidRuntime(8735):     ... 10 more
02-23 17:47:48.927: E/AndroidRuntime(8735): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.Fragment.instantiate(Fragment.java:618)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.Fragment.instantiate(Fragment.java:582)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.Activity.onCreateView(Activity.java:5282)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
02-23 17:47:48.927: E/AndroidRuntime(8735):     ... 19 more
02-23 17:47:48.927: E/AndroidRuntime(8735): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: DexPathList[[zip file "/data/app/com.juliette.basicgooglemap-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
02-23 17:47:48.927: E/AndroidRuntime(8735):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
02-23 17:47:48.927: E/AndroidRuntime(8735):     at android.app.Fragment.instantiate(Fragment.java:604)
02-23 17:47:48.927: E/AndroidRuntime(8735):     ... 23 more
02-23 17:47:48.927: E/AndroidRuntime(8735):     Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
02-23 17:47:48.927: E/AndroidRuntime(8735):         at java.lang.Class.classForName(Native Method)
02-23 17:47:48.927: E/AndroidRuntime(8735):         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
02-23 17:47:48.927: E/AndroidRuntime(8735):         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
02-23 17:47:48.927: E/AndroidRuntime(8735):         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
02-23 17:47:48.927: E/AndroidRuntime(8735):         ... 25 more
02-23 17:47:48.927: E/AndroidRuntime(8735):     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

这是清单

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

<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"/>
<!-- The following two permissions are not required to use
 Google Maps Android API v2, but are recommended. -->
<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"/>

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".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.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="API_KEY"/>

<uses-library android:name="com.google.android.maps" />
</application>

</manifest>

这是我的主要活动

package com.juliette.basicgooglemap;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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

和布局

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:name="com.google.android.gms.maps.SupportMapFragment"/>

非常感谢任何建议,我很困惑。

1 个答案:

答案 0 :(得分:2)

  1. 可能是因为未正确导入库,请按照此link

  2. 中的步骤操作
  3. 如果没有解决并且你已经正确设置了库,这可能是因为proguard可能已经剥离了播放服务。为避免它,请在proguard-project.txt

  4. 中添加以下内容
    -keep class * extends java.util.ListResourceBundle {
        protected Object[][] getContents();
    }
    
    -keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
        public static final *** NULL;
    }
    
    -keepnames @com.google.android.gms.common.annotation.KeepName class *
    -keepclassmembernames class * {
        @com.google.android.gms.common.annotation.KeepName *;
    }  
    -keepnames class * implements android.os.Parcelable {
        public static final ** CREATOR;
    }
    
    1. 您建议的Android清单修复
    2. 你可能没有添加密钥,我看到'API_KEY'字符串而不是密钥,它应该是这样的(显然密钥会有所不同)

      <meta-data
              android:name="com.google.android.maps.v2.API_KEY"
              android:value="AZzaSyDhkhNotUseFullKey49ylKD2bw1HM" />
      

      你在AndroidManifest.xml中遗漏了这些,

      <permission
          android:name="com.juliette.basicgooglemap.permission.MAPS_RECEIVE"
          android:protectionLevel="signature" />
      <uses-permission android:name="com.juliette.basicgooglemap.permission.MAPS_RECEIVE" />
      

      另外添加:

      <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
      

      接下来设置您的布局和代码,例如this帖子,您没有正确使用该片段。