我正在尝试使用 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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
这是活动中的代码:
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
public class MainActivity extends Activity {
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.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;
}
}
我正在尝试在我的设备上运行它(Andorid 4.1.2,即 api 16)我在我的项目中添加了 google-play-services-lib。当我运行应用程序时,它会崩溃。
这是完整的 logcat 日志:
07-23 12:55:30.955: E/AndroidRuntime(14098): FATAL EXCEPTION: main
07-23 12:55:30.955: E/AndroidRuntime(14098): java.lang.RuntimeException: Unable to start activity ComponentInfo{stringbind.nikolacjenolomacdemo/stringbind.nikolacjenolomacdemo.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.os.Looper.loop(Looper.java:137)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-23 12:55:30.955: E/AndroidRuntime(14098): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 12:55:30.955: E/AndroidRuntime(14098): at java.lang.reflect.Method.invoke(Method.java:511)
07-23 12:55:30.955: E/AndroidRuntime(14098): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-23 12:55:30.955: E/AndroidRuntime(14098): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-23 12:55:30.955: E/AndroidRuntime(14098): at dalvik.system.NativeStart.main(Native Method)
07-23 12:55:30.955: E/AndroidRuntime(14098): Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-23 12:55:30.955: E/AndroidRuntime(14098): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.Activity.setContentView(Activity.java:1924)
07-23 12:55:30.955: E/AndroidRuntime(14098): at stringbind.nikolacjenolomacdemo.MainActivity.onCreate(MainActivity.java:18)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.Activity.performCreate(Activity.java:5206)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-23 12:55:30.955: E/AndroidRuntime(14098): ... 11 more
07-23 12:55:30.955: E/AndroidRuntime(14098): 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
07-23 12:55:30.955: E/AndroidRuntime(14098): at maps.ah.ba.a(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098): at maps.ah.ba.a(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098): at maps.ah.an.a(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098): at maps.ah.bh.a(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098): at maps.ah.bg.a(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098): at bgl.onTransact(SourceFile:107)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.os.Binder.transact(Binder.java:326)
07-23 12:55:30.955: E/AndroidRuntime(14098): at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098): at com.google.android.gms.maps.MapFragment$a.onCreateView(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098): at com.google.android.gms.internal.bb$4.b(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098): at com.google.android.gms.internal.bb.a(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098): at com.google.android.gms.internal.bb.onCreateView(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098): at com.google.android.gms.maps.MapFragment.onCreateView(Unknown Source)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:807)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1013)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1112)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.app.Activity.onCreateView(Activity.java:4857)
07-23 12:55:30.955: E/AndroidRuntime(14098): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
07-23 12:55:30.955: E/AndroidRuntime(14098): ... 21 more
07-23 12:55:30.985: D/dalvikvm(14098): GC_CONCURRENT freed 285K, 7% free 12559K/13447K, paused 12ms+2ms, total 33ms
更新:
这是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="stringbind.nikolacjenolomacdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<uses-permission android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<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" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="**MY KEY**"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="stringbind.nikolacjenolomacdemo.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>