谷歌地图android API V2失败

时间:2014-02-17 16:06:27

标签: java android permissions maps

尝试将地图添加到Android应用程序中,我已经创建了API密钥并启用了Google Map Android V2;我在清单文件中添加了权限,google play服务版本和API密钥。我还在项目中添加了Google Play服务库。

但是当我启动它停止的应用程序时,我不知道为什么,我认为这与权限有关,但只是为了确保,我按照Google Developpers网站的建议复制/过去权限向我们展示如何添加地图。但就像我说的那样,我不确定这是不是真的。 这是我的代码:

清单文件:

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

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


<!-- ============  PERMISSIONS: ============ -->
<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"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- 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"/>

 <!-- ============ END PERMISSIONS =============== -->
            <!-- this will notify the user that the device needs OpenGL ES version 2 to display the map (as recomanded from the google developpers website)-->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar" >
<!-- ====   google play services version:  ====  -->
    <meta-data
         android:name="com.google.android.gms.version"
         android:value="@integer/google_play_services_version" />

        <activity
            android:name="usthb.appusthb.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>

<!-- Google Map Android API V2 === ==== ===  API key:  -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyCfqonheoI9OG_REcsnl7-xgmpiFwu692I"/>

    </application>

</manifest>

MainLayout:

<?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.MapFragment"/>

MainActivity:

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(this, "sup", 2000).show();
    }
}

日志:

02-17 10:50:11.928: E/AndroidRuntime(1356): FATAL EXCEPTION: main
02-17 10:50:11.928: E/AndroidRuntime(1356): Process: usthb.appusthb, PID: 1356
02-17 10:50:11.928: E/AndroidRuntime(1356): java.lang.RuntimeException: Unable to start activity ComponentInfo{usthb.appusthb/usthb.appusthb.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.os.Looper.loop(Looper.java:136)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at java.lang.reflect.Method.invokeNative(Native Method)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at java.lang.reflect.Method.invoke(Method.java:515)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at dalvik.system.NativeStart.main(Native Method)
02-17 10:50:11.928: E/AndroidRuntime(1356): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.Activity.setContentView(Activity.java:1929)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at usthb.appusthb.MainActivity.onCreate(MainActivity.java:12)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.Activity.performCreate(Activity.java:5231)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-17 10:50:11.928: E/AndroidRuntime(1356):     ... 11 more
02-17 10:50:11.928: E/AndroidRuntime(1356): Caused by: java.lang.SecurityException: The Maps API requires the additional following permissions to be set in the AndroidManifest.xml to ensure a correct behavior:
02-17 10:50:11.928: E/AndroidRuntime(1356): <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
02-17 10:50:11.928: E/AndroidRuntime(1356):     at maps.af.ch.a(Unknown Source)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at maps.af.ay.a(Unknown Source)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at maps.af.ay.a(Unknown Source)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at maps.af.al.a(Unknown Source)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at maps.af.bg.a(Unknown Source)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at maps.af.bf.a(Unknown Source)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at ebt.onTransact(SourceFile:107)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.os.Binder.transact(Binder.java:361)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at com.google.android.gms.maps.MapFragment$a.onCreateView(Unknown Source)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at com.google.android.gms.dynamic.a$4.b(Unknown Source)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at com.google.android.gms.dynamic.a.a(Unknown Source)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.Fragment.performCreateView(Fragment.java:1700)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.app.Activity.onCreateView(Activity.java:4786)
02-17 10:50:11.928: E/AndroidRuntime(1356):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
02-17 10:50:11.928: E/AndroidRuntime(1356):     ... 20 more

1 个答案:

答案 0 :(得分:1)

更改此

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

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

因为你的min sdk是11.所以你需要使用SupportMapFragment。 12岁及以上使用MapFragment