【问题标题】:When i use Google Map Api v2 for map then i can't add google play service library当我将 Google Map Api v2 用于地图时,我无法添加 google play 服务库
【发布时间】:2013-11-28 21:23:32
【问题描述】:

我想使用 Google Map v2 在地图中显示当前位置。我创建 sha 密钥并生成 API 密钥并与 androidmanifest.xml 集成。我下载以下链接的示例:

http://wptrafficanalyzer.in/blog/showing-current-location-in-google-maps-using-api-v2-with-supportmapfragment/

我还授予互联网权限和其他权限,但在我的 log-cat 中显示以下错误。

11-20 11:09:59.190: E/dalvikvm(719): Could not find class 'com.google.android.gms.maps.model.LatLng', referenced from method in.wptrafficanalyzer.locationingooglemapv2.MainActivity.onLocationChanged
11-20 11:09:59.190: W/dalvikvm(719): VFY: unable to resolve new-instance 460 (Lcom/google/android/gms/maps/model/LatLng;) in Lin/wptrafficanalyzer/locationingooglemapv2/MainActivity;
11-20 11:09:59.190: D/dalvikvm(719): VFY: replacing opcode 0x22 at 0x0010
11-20 11:09:59.220: D/dalvikvm(719): DexOpt: unable to opt direct call 0x0cdc at 0x12 in Lin/wptrafficanalyzer/locationingooglemapv2/MainActivity;.onLocationChanged
11-20 11:09:59.482: D/AndroidRuntime(719): Shutting down VM
11-20 11:09:59.482: W/dalvikvm(719): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-20 11:09:59.501: E/AndroidRuntime(719): FATAL EXCEPTION: main
11-20 11:09:59.501: E/AndroidRuntime(719): java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.locationingooglemapv2/in.wptrafficanalyzer.locationingooglemapv2.MainActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.os.Looper.loop(Looper.java:137)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-20 11:09:59.501: E/AndroidRuntime(719):  at java.lang.reflect.Method.invokeNative(Native Method)
11-20 11:09:59.501: E/AndroidRuntime(719):  at java.lang.reflect.Method.invoke(Method.java:511)
11-20 11:09:59.501: E/AndroidRuntime(719):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-20 11:09:59.501: E/AndroidRuntime(719):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-20 11:09:59.501: E/AndroidRuntime(719):  at dalvik.system.NativeStart.main(Native Method)
11-20 11:09:59.501: E/AndroidRuntime(719): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-20 11:09:59.501: E/AndroidRuntime(719):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.app.Activity.setContentView(Activity.java:1867)
11-20 11:09:59.501: E/AndroidRuntime(719):  at in.wptrafficanalyzer.locationingooglemapv2.MainActivity.onCreate(MainActivity.java:27)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.app.Activity.performCreate(Activity.java:5008)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-20 11:09:59.501: E/AndroidRuntime(719):  ... 11 more
11-20 11:09:59.501: E/AndroidRuntime(719): 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
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
11-20 11:09:59.501: E/AndroidRuntime(719):  ... 21 more
11-20 11:09:59.501: E/AndroidRuntime(719): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
11-20 11:09:59.501: E/AndroidRuntime(719):  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
11-20 11:09:59.501: E/AndroidRuntime(719):  at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-20 11:09:59.501: E/AndroidRuntime(719):  at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-20 11:09:59.501: E/AndroidRuntime(719):  at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
11-20 11:09:59.501: E/AndroidRuntime(719):  ... 24 more

现在我可以做什么来显示地图?

【问题讨论】:

  • 你添加google play服务库了吗...
  • 是的,但是添加google play服务库时显示错误。
  • 我无法在项目中添加外部库。这是什么原因?
  • Google play 服务库应该在示例目录中...点击链接docs.google.com/document/…
  • 我知道我已经从以下路径导入了 google play 库:android-sdk->extras->google->google_play_services->libproject。

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


【解决方案1】:

地图活动

public class MainActivity extends FragmentActivity {


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



}

activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <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"/>
</RelativeLayout>

清单文件

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

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

    <permission
        android:name="YourPackageName.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="YourPackageName.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="YourPackageName.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="4030500" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="YourKey" />
    </application>`enter code here`

</manifest>

打开sdk manager,在extras下下载google play service,会得到google-play-services_lib.jar和google-play-services.jar两个库。您可以在额外的 google play 服务下找到这些库。您必须将这两个库复制并粘贴到应用中的 lib 文件夹下,并将这些库添加到 java Build 路径下。

【讨论】:

  • 我已经使用了此代码,但生成了以下错误:“类:com.google.android.gms.maps.SupportMapFragment 找不到。
  • 当我使用示例代码使用“developers.google.com/maps/documentation/android/start”链接显示地图时,logcat 中显示以下错误:FATAL EXCEPTION: main, java.lang.RuntimeException: Unable to start activity ComponentInfo{com. example.mapimpletation/com.example.mapimpletation.MainActivity}:android.view.InflateException: Binary XML file line #7: Error inflating class fragment。”我该怎么办?
  • 我现在实现你的源代码,我用 代替 在 androidmenifest.xml 文件中?
  • 我只是重新启动我的 Eclipse 并在控制台中显示以下错误:LocationGoogleMapV2Demo] 此时所有版本的库必须相同。 ,找到的版本是: ,路径: D:\october2013_Workspace\google_play_services\libproject\google-play-services_lib\libs\google-play-services.jar 长度: 380917 SHA-1: 19584d79c3c85315cbd9227c1b7abe781ab1ae76 路径: \LocationGoogleMapV2Demo\libs\google-play -services.jar 长度:959413,SHA-1:cebe8e4a3a469c4623addcca1dc571407ff3a08e Jar 不匹配!修复你的依赖项
  • 地图屏幕显示在我的实际设备中,但地图未加载。那我能做什么呢?
【解决方案2】:

确保通过转到 google api 控制台转到 services 在下去你会发现 google map v2 可以使用它... 要添加对库项目的引用,请按以下步骤操作:

    1. Make sure that both the project library and the application project that depends on it are in your workspace. If one of the projects is missing, import it into your workspace.
    2. In the Package Explorer, right-click the dependent project and select Properties.
    3.In the Properties window, select the "Android" properties group at left and locate the Library properties at right.
    4.Click Add to open the Project Selection dialog.
    5.From the list of available library projects, select a project and click OK.
    6.When the dialog closes, click Apply in the Properties window.
    7.Click OK to close the Properties window.

如何添加参考谷歌播放库

Android Library Projects

【讨论】:

  • 确保项目库和依赖它的应用程序项目都在您的工作区中。如果缺少其中一个项目,请将其导入您的工作区。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多