[英]Android 2.3.3 and google maps api v2 error starting activity
[英]Error with using google maps api v2 in Android
我使用Google Maps API v2創建了一個應用程序視圖地圖。 這是我的代碼:
我的活動
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
}
我的main_activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
Android清單:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyB96b4lWziSteDtMTqrDDurcTvOIwzKCRE" />
當我運行時,這是logcat:
07-21 17:53:33.734: E/AndroidRuntime(3558): FATAL EXCEPTION: main
07-21 17:53:33.734: E/AndroidRuntime(3558): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demogooglemap/com.example.demogooglemap.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.app.ActivityThread.access$700(ActivityThread.java:151)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.os.Looper.loop(Looper.java:137)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.app.ActivityThread.main(ActivityThread.java:5293)
07-21 17:53:33.734: E/AndroidRuntime(3558): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 17:53:33.734: E/AndroidRuntime(3558): at java.lang.reflect.Method.invoke(Method.java:511)
07-21 17:53:33.734: E/AndroidRuntime(3558): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-21 17:53:33.734: E/AndroidRuntime(3558): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-21 17:53:33.734: E/AndroidRuntime(3558): at dalvik.system.NativeStart.main(Native Method)
07-21 17:53:33.734: E/AndroidRuntime(3558): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
07-21 17:53:33.734: E/AndroidRuntime(3558): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
請幫忙!
您的最低SDK為8
android:minSdkVersion="8"
在api級別11以下,您需要使用SupportMapFragment
並擴展FragmentActivity
。
改變這個
android:name="com.google.android.gms.maps.MapFragment"
至
android:name="com.google.android.gms.maps.SupportMapFragment"
改變這個
public class MainActivity extends Activity
至
public class MainActivity extends FragmentActivity
清單文件中缺少少量權限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 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"/>
跟隨
只需添加@Raghunandan所說的內容,您還需要確保設備知道您所使用的Google Play服務的版本。 還要在您的計划中實現這一點(在應用程序內部)。
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
還要確保已將google-play-services_lib導入到工作區中,並聲明您的項目正在使用服務(在該項目的android屬性中)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.