gpt4 book ai didi

java - 谷歌地图 android API V2 失败

转载 作者:行者123 更新时间:2023-11-30 03:05:47 24 4
gpt4 key购买 nike

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

但是当我启动应用程序时它停止了,我不知道为什么,我认为这与权限有关,但为了确保,我按照 Google Developpers 网站的建议复制/粘贴了权限向我们展示如何添加 map 。但正如我所说,我不确定这是否是真正的交易。这是我的代码:

list 文件:

<?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>

主要布局:

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

主要 Activity :

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

最佳答案

改变这个

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

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

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

关于java - 谷歌地图 android API V2 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21833844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com