简体   繁体   English

在Android中使用Google Maps API v2时出错

[英]Error with using google maps api v2 in Android

I create an app view maps using google maps api v2. 我使用Google Maps API v2创建了一个应用程序视图地图。 This is my code: 这是我的代码:

My Activity 我的活动

public class MainActivity extends Activity {

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

My main_activity: 我的main_activity:

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

    <fragment 
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment" />
</LinearLayout>

Android manifest: Android清单:

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

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

    <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.maps.v2.API_KEY"
            android:value="AIzaSyB96b4lWziSteDtMTqrDDurcTvOIwzKCRE" />

And when I running, this is logcat: 当我运行时,这是logcat:

    07-21 17:53:33.734: E/AndroidRuntime(3558): FATAL EXCEPTION: main
    07-21 17:53:33.734: E/AndroidRuntime(3558): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demogooglemap/com.example.demogooglemap.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at android.app.ActivityThread.access$700(ActivityThread.java:151)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at android.os.Handler.dispatchMessage(Handler.java:99)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at android.os.Looper.loop(Looper.java:137)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at android.app.ActivityThread.main(ActivityThread.java:5293)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at java.lang.reflect.Method.invokeNative(Native Method)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at java.lang.reflect.Method.invoke(Method.java:511)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at dalvik.system.NativeStart.main(Native Method)
    07-21 17:53:33.734: E/AndroidRuntime(3558): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    07-21 17:53:33.734: E/AndroidRuntime(3558):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)

Plz help! 请帮忙!

Your min sdk is 8 您的最低SDK为8

android:minSdkVersion="8"

Below api level 11 you need to use SupportMapFragment and extend FragmentActivity . 在api级别11以下,您需要使用SupportMapFragment并扩展FragmentActivity

Change this 改变这个

android:name="com.google.android.gms.maps.MapFragment" 

to

android:name="com.google.android.gms.maps.SupportMapFragment" 

Change this 改变这个

public class MainActivity extends Activity 

to

public class MainActivity extends FragmentActivity 

Missing few permissions in manifest file 清单文件中缺少少量权限

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

Follow 跟随

https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2 https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2

Just adding on to what @Raghunandan has said, you also need to make sure the device knows what version of google play services you are using. 只需添加@Raghunandan所说的内容,您还需要确保设备知道您所使用的Google Play服务的版本。 implement this into your manafest too (inside application). 还要在您的计划中实现这一点(在应用程序内部)。

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

Also make sure you have imported google-play-services_lib into your workspace and declared that your project is using the services (in the android properties of said project). 还要确保已将google-play-services_lib导入到工作区中,并声明您的项目正在使用服务(在该项目的android属性中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM