我正在尝试使用map api v2实现地图。 我在这里搜索了我的问题的答案,但没有找到帮助我的正确答案。 任何帮助表示赞赏。谢谢。
我的xml代码:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SupportMapFragmentActivity" >
<fragment
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="com.example.googlemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<permission
android:name="com.truiton.supportmapfragment.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<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"/>
<uses-library android:name="com.google.android.maps" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.googlemap.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.maps.v2.API_KEY"
android:value="My API Key"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
主要: 包com.example.googlemap;
import com.google.android.gms.internal.fm;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GoogleMap map;
FragmentManager fmanager = getSupportFragmentManager();
Fragment fragment = fmanager.findFragmentById(R.id.map);
SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
map = supportmapfragment.getMap();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
然后我得到了那些错误:
02-16 17:09:39.014: E/AndroidRuntime(13283): FATAL EXCEPTION: main
02-16 17:09:39.014: E/AndroidRuntime(13283): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemap/com.example.googlemap.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.ActivityThread.access$600(ActivityThread.java:138)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.os.Looper.loop(Looper.java:213)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.ActivityThread.main(ActivityThread.java:4787)
02-16 17:09:39.014: E/AndroidRuntime(13283): at java.lang.reflect.Method.invokeNative(Native Method)
02-16 17:09:39.014: E/AndroidRuntime(13283): at java.lang.reflect.Method.invoke(Method.java:511)
02-16 17:09:39.014: E/AndroidRuntime(13283): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
02-16 17:09:39.014: E/AndroidRuntime(13283): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
02-16 17:09:39.014: E/AndroidRuntime(13283): at dalvik.system.NativeStart.main(Native Method)
02-16 17:09:39.014: E/AndroidRuntime(13283): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-16 17:09:39.014: E/AndroidRuntime(13283): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:262)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.Activity.setContentView(Activity.java:1867)
02-16 17:09:39.014: E/AndroidRuntime(13283): at com.example.googlemap.MainActivity.onCreate(MainActivity.java:12)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.Activity.performCreate(Activity.java:5008)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
02-16 17:09:39.014: E/AndroidRuntime(13283): ... 11 more
02-16 17:09:39.014: E/AndroidRuntime(13283): Caused by: java.lang.RuntimeException: API key not found. Check that <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
02-16 17:09:39.014: E/AndroidRuntime(13283): at maps.e.ay.a(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at maps.e.ay.a(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at maps.e.ay.a(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at maps.e.ay.a(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at maps.e.al.a(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at maps.e.bg.a(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at maps.e.bf.a(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at eio.onTransact(SourceFile:107)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.os.Binder.transact(Binder.java:326)
02-16 17:09:39.014: E/AndroidRuntime(13283): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at com.google.android.gms.maps.MapFragment$a.onCreateView(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at com.google.android.gms.dynamic.a$4.b(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at com.google.android.gms.dynamic.a.a(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at com.google.android.gms.dynamic.a.onCreateView(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:811)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1017)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1116)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.app.Activity.onCreateView(Activity.java:4664)
02-16 17:09:39.014: E/AndroidRuntime(13283): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
02-16 17:09:39.014: E/AndroidRuntime(13283): ... 20 more
如果你能帮助我,我很高兴!