I create an app view maps using google maps api v2. This is my code:
My Activity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
}
My 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 manifest:
<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" />
And when I running, this is 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)
Plz help!
Your min sdk is 8
android:minSdkVersion="8"
Below api level 11 you need to use SupportMapFragment
and extend FragmentActivity
.
Change this
android:name="com.google.android.gms.maps.MapFragment"
to
android:name="com.google.android.gms.maps.SupportMapFragment"
Change this
public class MainActivity extends Activity
to
public class MainActivity extends FragmentActivity
Missing few permissions in manifest file
<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"/>
Follow
Just adding on to what @Raghunandan has said, you also need to make sure the device knows what version of google play services you are using. implement this into your manafest too (inside application).
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Also make sure you have imported google-play-services_lib into your workspace and declared that your project is using the services (in the android properties of said project).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.