Android 在我的应用程序中显示空地图
Android 在我的应用程序中显示空地图,android,eclipse,google-maps,android-fragments,google-play-services,Android,Eclipse,Google Maps,Android Fragments,Google Play Services,我正在做一个应用程序,使用谷歌地图,但地图没有显示,我看到的只有空白方块 这是我的舱单: <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="fossa.damiano.mapsexample" android:versionCode="1" android:versionName="1.0" > <uses-sdk andr
我正在做一个应用程序,使用谷歌地图,但地图没有显示,我看到的只有空白方块 这是我的舱单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fossa.damiano.mapsexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<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>
</application>
</manifest>
以及main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<com.google.android.maps.MapView
android:id="@+id/mapview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="My Key" //I've already generated my Key
android:clickable="true" />
</RelativeLayout>
我已经启用了internet连接,因此无法找出问题所在
更新我已将谷歌地图的版本从1更新为2。
以下是更改:
舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pocket_city"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<permission
android:name="com.example.pocket_city.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.pocket_city.permission.MAPS_RECEIVE"/>
<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"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.pocket_city.Home"
android:configChanges="keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MonumentMapActivity"
android:label="@string/title_activity_monument_map"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxx"/>
</application>
</manifest>
以及xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
更新2我做了另一个应用程序,只显示地图,它的工作
但是,当我尝试将该代码添加到我的应用程序时,它给出了上面显示的相同错误
那么,为什么映射不能同时在两个应用程序上工作呢?从不推荐的API v1迁移到API v2的最简单方法是
MapFragment
类的使用:
仅当您的目标是API 12及以上版本时才使用此类。否则,请使用SupportMapFragment
你的min SDK是11,因此你应该使用
SupportMapFragment
或更改你的min SDK级别。第一,你使用的是不推荐版本的google maps(v1),IIRC他们不接受它的任何新密钥,你应该使用maps v2。我以为我使用的是第二版本的maps。如何修复?v2在google play services中,您需要将google play services作为库项目导入到您的项目中才能使用它。搜索堆栈溢出关于如何做我已经更新了代码。看到这个答案吗
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pocket_city"
android:versionCode="1"
android:versionName="1.0" >
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<permission
android:name="com.example.pocket_city.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.pocket_city.permission.MAPS_RECEIVE"/>
<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"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.pocket_city.Home"
android:configChanges="keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MonumentMapActivity"
android:label="@string/title_activity_monument_map"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxx"/>
</application>
</manifest>
package com.example.pocket_city;
import android.app.Activity;
import android.os.Bundle;
public class MonumentMapActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.monument_map);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
06-01 10:12:10.496: E/AndroidRuntime(28178): FATAL EXCEPTION: main
06-01 10:12:10.496: E/AndroidRuntime(28178): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pocket_city/com.example.pocket_city.MonumentMapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.ActivityThread.access$700(ActivityThread.java:140)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.os.Looper.loop(Looper.java:137)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.ActivityThread.main(ActivityThread.java:4921)
06-01 10:12:10.496: E/AndroidRuntime(28178): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 10:12:10.496: E/AndroidRuntime(28178): at java.lang.reflect.Method.invoke(Method.java:511)
06-01 10:12:10.496: E/AndroidRuntime(28178): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
06-01 10:12:10.496: E/AndroidRuntime(28178): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
06-01 10:12:10.496: E/AndroidRuntime(28178): at dalvik.system.NativeStart.main(Native Method)
06-01 10:12:10.496: E/AndroidRuntime(28178): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-01 10:12:10.496: E/AndroidRuntime(28178): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.Activity.setContentView(Activity.java:1924)
06-01 10:12:10.496: E/AndroidRuntime(28178): at com.example.pocket_city.MonumentMapActivity.onCreate(MonumentMapActivity.java:11)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.Activity.performCreate(Activity.java:5206)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-01 10:12:10.496: E/AndroidRuntime(28178): ... 11 more
06-01 10:12:10.496: E/AndroidRuntime(28178): Caused by: android.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
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.Fragment.instantiate(Fragment.java:584)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.Fragment.instantiate(Fragment.java:552)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.Activity.onCreateView(Activity.java:4849)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-01 10:12:10.496: E/AndroidRuntime(28178): ... 21 more
06-01 10:12:10.496: E/AndroidRuntime(28178): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.SupportMapFragment
06-01 10:12:10.496: E/AndroidRuntime(28178): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-01 10:12:10.496: E/AndroidRuntime(28178): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-01 10:12:10.496: E/AndroidRuntime(28178): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-01 10:12:10.496: E/AndroidRuntime(28178): at android.app.Fragment.instantiate(Fragment.java:574)
06-01 10:12:10.496: E/AndroidRuntime(28178): ... 24 more