Reputation: 277
I am not able to figure out what am I missing. I am trying to implement a simple Google Map in my Main Activity. A google map should simply be displayed in the MainActivity.
Google API Key is correctly updated in the Manifest file. I have google-play-services_lib.jar included in my project. Google API 18 was selected as "Compile with" while creating a new Android Project in Eclipse.
Code in MainActivity.java
package com.example.maptest;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Code in activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Code in my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maptest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<permission
android:name="com.example.googlemaps.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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" >
<activity
android:name="com.example.maptest.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="MyActualKey" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
Logcat
02-26 22:50:40.075: D/AndroidRuntime(14428): Shutting down VM
02-26 22:50:40.075: W/dalvikvm(14428): threadid=1: thread exiting with uncaught exception (group=0x41465700)
02-26 22:50:40.177: E/AndroidRuntime(14428): FATAL EXCEPTION: main
02-26 22:50:40.177: E/AndroidRuntime(14428): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.maptest/com.example.maptest.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.os.Looper.loop(Looper.java:137)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.ActivityThread.main(ActivityThread.java:5103)
02-26 22:50:40.177: E/AndroidRuntime(14428): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 22:50:40.177: E/AndroidRuntime(14428): at java.lang.reflect.Method.invoke(Method.java:525)
02-26 22:50:40.177: E/AndroidRuntime(14428): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-26 22:50:40.177: E/AndroidRuntime(14428): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-26 22:50:40.177: E/AndroidRuntime(14428): at dalvik.system.NativeStart.main(Native Method)
02-26 22:50:40.177: E/AndroidRuntime(14428): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-26 22:50:40.177: E/AndroidRuntime(14428): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.Activity.setContentView(Activity.java:1895)
02-26 22:50:40.177: E/AndroidRuntime(14428): at com.example.maptest.MainActivity.onCreate(MainActivity.java:13)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.Activity.performCreate(Activity.java:5133)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-26 22:50:40.177: E/AndroidRuntime(14428): ... 11 more
02-26 22:50:40.177: E/AndroidRuntime(14428): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
02-26 22:50:40.177: E/AndroidRuntime(14428): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
02-26 22:50:40.177: E/AndroidRuntime(14428): ... 21 more
Upvotes: 0
Views: 1724
Reputation: 20934
1) You don't have meta-tag specified for google play services version. There should be 2 meta-tags - your map key and play services version:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
2) Android Emulator doesn't have GooglePlayServices installed. Use real device
UPDATE
3) Since you use FragmentActivity
from support library, you have to use support map fragment as well. Replace your activity_main
layout with this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Upvotes: 1