我正在尝试使用 maps api v2 for android 实现一个应用程序。这是我正在使用的代码
<?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.SupportMapFragment"/>
主要活动是
package com.example.nav3;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
public class MainActivity2 extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
}
@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_activity2, menu);
return true;
}
}
清单是这个
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nav3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<permission
android:name="com.example.nav3.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.example.nav3.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" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.nav3.MainActivity2"
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 key comes here" />
</application>
</manifest>
不过,在运行该应用程序时,我只看到缩放控件,但看不到任何地图。为什么会这样?谢谢
堆栈跟踪
08-24 22:58:41.482: D/AndroidRuntime(3083): Shutting down VM
08-24 22:58:41.482: W/dalvikvm(3083): threadid=1: thread exiting with uncaught exception (group=0x41552700)
08-24 22:58:41.502: D/dalvikvm(3083): GC_FOR_ALLOC freed 285K, 4% free 9093K/9404K, paused 15ms, total 15ms
08-24 22:58:41.512: E/AndroidRuntime(3083): FATAL EXCEPTION: main
08-24 22:58:41.512: E/AndroidRuntime(3083): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nav3/com.example.nav3.MainActivity2}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.os.Looper.loop(Looper.java:137)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-24 22:58:41.512: E/AndroidRuntime(3083): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 22:58:41.512: E/AndroidRuntime(3083): at java.lang.reflect.Method.invoke(Method.java:525)
08-24 22:58:41.512: E/AndroidRuntime(3083): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-24 22:58:41.512: E/AndroidRuntime(3083): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-24 22:58:41.512: E/AndroidRuntime(3083): at dalvik.system.NativeStart.main(Native Method)
08-24 22:58:41.512: E/AndroidRuntime(3083): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-24 22:58:41.512: E/AndroidRuntime(3083): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.app.Activity.setContentView(Activity.java:1895)
08-24 22:58:41.512: E/AndroidRuntime(3083): at com.example.nav3.MainActivity2.onCreate(MainActivity2.java:13)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.app.Activity.performCreate(Activity.java:5133)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-24 22:58:41.512: E/AndroidRuntime(3083): ... 11 more
08-24 22:58:41.512: E/AndroidRuntime(3083): Caused by: java.lang.IllegalStateException: Fragment com.google.android.gms.maps.MapFragment did not create a view.
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.app.Activity.onCreateView(Activity.java:4769)
08-24 22:58:41.512: E/AndroidRuntime(3083): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
08-24 22:58:41.512: E/AndroidRuntime(3083): ... 20 more