Unable instantiate android.gms.maps.MapFragment
Solution 1
In IntelliJ IDEA (updated for IntelliJ 12):
- Create a file
~/android-sdk/extras/google/google_play_services/libproject/google-play-services_lib/src/dummy.java
containingclass dummy {}
. - File->Import Module->
~/android-sdk/extras/google/google_play_services/libproject/google-play-services_lib
- Create Module from Existing Sources
- Next->Next->Next->Next->Finish
- File->Project Structure->Modules->YourApp
- +->Module Dependency->Google-play-services_lib (The + button is in the top right corner of the dialog.)
-
+->Jars or directories->
~/android-sdk/extras/google/google_play_services/libproject/google-play-services_lib/libs/google-play-services.jar
- Use the up/down arrows to move
<Module source>
to the bottom of the list.
You can delete dummy.java
if you like.
Edit: After using this for a while I've found that there is a small flaw/bug. IDEA will sometimes complain about not being able to open a .iml
project file in the google-play-services_lib
directory, despite the fact that you never told it there was a project there. If that happens, rebuilding the project solves the problem, at least until it comes back.
Solution 2
Update
Please follow Commonsware MapV2 code snippets to get better understanding.
(It is present in Omnibus edition)
Following snippet is working fine at my end.I choose to use SupportMapFragment
.
Dont forget to add google-play-services.jar
into your project.
MainActivity.java
package com.example.newmapview;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SupportMapFragment fragment = new SupportMapFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newmapview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<permission
android:name="com.example.newmapview.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.newmapview.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<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-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.newmapview.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="XXXXX" />
</application>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
</manifest>
Here is the result
Hope this will help.
Solution 3
Just try to replace your layout with :
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
You need to use the SupportMapFragment for API under 11 !
Aurel
Solution 4
I faced the same problem ant it took me tow days to figure out a solution that worked for me :
- Delete the project
google-play-services_lib
(right click on the project delete ) - Delete the project containing the Google maps demo (
MainActivity
in my case ) if you have one - Copy the project google-play-services_lib( extras\google\google_play_services\libproject\google-play-services_lib) into your workspace then import it as General project (File->import->existing projects into workspase )
- Right click on your project ( in which you want to load the map ) -> Android -> add (under library ) google-play-services_lib
You should see something like this :
Note : You should not have something like this ( the project should be referred from your workspace ):
I think that the problem is that tow projects are referencing the same library
Solution 5
try this
http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject
I just added the project of google services and added a reference in my project property->Android
Related videos on Youtube
Comments
-
mmm2006 over 4 years
I try to do a demo with google maps android v2 with very simple activity, just copy code from google page: https://developers.google.com/maps/documentation/android/start#adding_the_api_key_to_your_application
for activity:
package com.example.mapdemo; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
for layout:
<?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" class="com.google.android.gms.maps.MapFragment"/>
I had apply for a api key according to page and modify my androidmanifest.xml file, just like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wenhai.driverschool" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET" /> <!-- add for map2 --> <permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <!-- External storage for caching. --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- Maps API needs OpenGL ES 2.0. --> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <application 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="AIzaSyDVAF4WaVSVRDKJx87It8OSFP5txQcPabc" /> <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
I also reference my app to google-play-services_lib in eclipse.
but everytime, error report in logcat like this:
2-05 16:22:53.609: E/AndroidRuntime(21623): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wenhai.driverschool/com.wenhai.driverschool.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.ActivityThread.access$600(ActivityThread.java:127) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.os.Handler.dispatchMessage(Handler.java:99) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.os.Looper.loop(Looper.java:137) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.ActivityThread.main(ActivityThread.java:4441) 12-05 16:22:53.609: E/AndroidRuntime(21623): at java.lang.reflect.Method.invokeNative(Native Method) 12-05 16:22:53.609: E/AndroidRuntime(21623): at java.lang.reflect.Method.invoke(Method.java:511) 12-05 16:22:53.609: E/AndroidRuntime(21623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) 12-05 16:22:53.609: E/AndroidRuntime(21623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 12-05 16:22:53.609: E/AndroidRuntime(21623): at dalvik.system.NativeStart.main(Native Method) 12-05 16:22:53.609: E/AndroidRuntime(21623): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 12-05 16:22:53.609: E/AndroidRuntime(21623): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Activity.setContentView(Activity.java:1835) 12-05 16:22:53.609: E/AndroidRuntime(21623): at com.wenhai.driverschool.MainActivity.onCreate(MainActivity.java:11) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Activity.performCreate(Activity.java:4465) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) 12-05 16:22:53.609: E/AndroidRuntime(21623): ... 11 more 12-05 16:22:53.609: E/AndroidRuntime(21623): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Fragment.instantiate(Fragment.java:581) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Fragment.instantiate(Fragment.java:549) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Activity.onCreateView(Activity.java:4235) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673) 12-05 16:22:53.609: E/AndroidRuntime(21623): ... 20 more 12-05 16:22:53.609: E/AndroidRuntime(21623): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment 12-05 16:22:53.609: E/AndroidRuntime(21623): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 12-05 16:22:53.609: E/AndroidRuntime(21623): at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 12-05 16:22:53.609: E/AndroidRuntime(21623): at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 12-05 16:22:53.609: E/AndroidRuntime(21623): at android.app.Fragment.instantiate(Fragment.java:571) 12-05 16:22:53.609: E/AndroidRuntime(21623): ... 23 more
I don't know the reason for this.
If i add google-play-services.jar into my project, it will report another error:
12-05 16:34:23.269: E/AndroidRuntime(22638): FATAL EXCEPTION: main 12-05 16:34:23.269: E/AndroidRuntime(22638): java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable 12-05 16:34:23.269: E/AndroidRuntime(22638): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source) 12-05 16:34:23.269: E/AndroidRuntime(22638): at com.google.android.gms.maps.MapFragment.onInflate(Unknown Source) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.Activity.onCreateView(Activity.java:4242) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.view.LayoutInflater.inflate(LayoutInflater.java:466) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 12-05 16:34:23.269: E/AndroidRuntime(22638): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:255) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.Activity.setContentView(Activity.java:1835) 12-05 16:34:23.269: E/AndroidRuntime(22638): at com.wenhai.driverschool.MainActivity.onCreate(MainActivity.java:11) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.Activity.performCreate(Activity.java:4465) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.ActivityThread.access$600(ActivityThread.java:127) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.os.Handler.dispatchMessage(Handler.java:99) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.os.Looper.loop(Looper.java:137) 12-05 16:34:23.269: E/AndroidRuntime(22638): at android.app.ActivityThread.main(ActivityThread.java:4441) 12-05 16:34:23.269: E/AndroidRuntime(22638): at java.lang.reflect.Method.invokeNative(Native Method) 12-05 16:34:23.269: E/AndroidRuntime(22638): at java.lang.reflect.Method.invoke(Method.java:511) 12-05 16:34:23.269: E/AndroidRuntime(22638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) 12-05 16:34:23.269: E/AndroidRuntime(22638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 12-05 16:34:23.269: E/AndroidRuntime(22638): at dalvik.system.NativeStart.main(Native Method)
Anyone can help me about this?
-
jumper0k over 11 yearsI'm getting the same error with MapView. I added the library and I did everything as described on this page developers.google.com/maps/documentation/android/start but all of this was useless for me.
-
nilkash over 11 yearsSame error.. any solution...
-
rikas over 11 yearsI'm having the same error and couldn't find any good solution yet :(
-
Tofeeq Ahmad about 11 yearsFor see these steps as i guess its unresolved yet techhomeblog.wordpress.com/2013/02/23/…
-
IgorGanapolsky about 11 yearsTry right-clicking on your project in Eclipse, select Android Tools > Add Support Library. In my case it was a stale version taken from ActionBarSherlock.
-
user821863 about 11 yearsstackoverflow.com/questions/13691028/… Ramz's answer says it all. Solved it for me!
-
-
Dan J over 11 yearsThis was the critical fix for me, none of the other answers above helped. The difference here is actually pretty subtle - I was adding it as a dependent project through "Java Build Path", when in fact I should have been doing it through the "Android" menu. I've added that info to one of my other answers: stackoverflow.com/questions/13733911/…
-
mmm2006 over 11 yearssorry for that, but it cannot work too in my place. error report this time:Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists, is public, and has an empty constructor that is public. I can find SupportMapFragment in jar, but it cannot work. i don't know the reason.
-
mmm2006 over 11 yearshi, Vipul Shah, thanks. I fond message from concole:[2012-12-06 10:20:26 - google-play-services_lib] Could not find google-play-services_lib.apk! i don't know whether this apk missing lead to my error. Can you tell me how to reference google-play-services.jar. just add it like below link:developer.android.com/intl/zh-CN/tools/projects/…. or add it in eclipse like below: Propertyes->java Build Path->Projects, add google-play-services_lib project. which one in your side? thanks.
-
Vipul over 11 years@mmm2006 I am afraid you might be running this application on android emulator.I am not sure but you might be facing this error because there is no play store app on emulator.Try it on actual device.
-
Aurel over 11 yearsHum, it seems that you need to do something like that: 1. Download the Google Play Services SDK on the Android SDK Mananger. 2. Import the project library in Eclipse. 3. Add the library to your project. 4. Be sure to have the android-support-v4.jar in your project (also available in the Android SDK Manager). 5. Create a fragment and add the name (' class="com.google.android.gms.maps.MapFragment"/>' for SDK >= 11 and 'class="com.google.android.gms.maps.MapFragment"/>' for SDK < 11). 6. Don't forget to try the sample code in a device (your app needs Google Play !)
-
Timmmm over 11 yearsWhat category do you use for the directory (classes, jar directory, sources, etc.)?
-
jumper0k over 11 yearsIn Intellij Idea I'm adding google-play-services_lib like a module. What is more, Intellij Idea doesn't recognize it like a module, because src folder is empty. You should add a java file with any code into src folder, after that create module from existing sources and add it to the project in project structure. Then clear the java file.
-
mmm2006 over 11 yearsok.I found i must follow this guide to refer google play service. developer.android.com/intl/zh-CN/tools/projects/…. but exmaple provied by google can work yet by now. :(
-
mmm2006 over 11 yearsIn fact, i run it in my phone. a sony phone.
-
Harsha M V over 11 yearsany idea whats the View Equivalent to the Framgament to put it inside the fragments xml file ?
-
Veer over 11 years@mmm2006: you need to extend "FragmentActivity" & not "Activity" then it should work and will not throw exception..
-
Shrikant Ballal over 11 yearsHi Vipul, can you tell me, how can I show my markers on map by using MapFragment? I have a list of latitudes and longitudes, I want to show them on map, I tried MapView but didn't work for me so I can't use ItemizedOverlay, MapFragment worked, so how to show multiple markers on MapFragment. If possible, provide me a link that has working code.
-
joseph.dev89 over 11 yearsalso it´s important too which api you are using, api 10 or less need support library developer.android.com/tools/extras/support-library.html exactly wich is you error?
-
shmoula about 11 yearsOMG! Why is this note so little at the mainpage and not big red on every page? I just f*ed almost whole day trying to get that running!!!!
-
Rahmathullah M about 11 yearshi, i cannot find the
File > Import Module
option in myEclipse IDE
. PLease help me... -
Timmmm about 11 yearsThis is for the superior IDE "IntelliJ IDEA".
-
lomza about 11 yearsThanks for your answer! But it was enough for me to add module AND jar dependency in the project and everything worked!
-
Nick about 11 yearsFor step 6 in Intellij 12, the + button is in the bottom left below the third pane.
-
Timmmm about 11 years@Nick: You mean 11? They moved it from there to the top-right in version 12. Check the edit I made to this answer.
-
Nick about 11 years@Timmmm, maybe it's different on the mac. It's just how you described it on my Windows box.
-
Paschalis about 11 years@Aurel is there i way i can + more than 1 your answer? This is what i was looking for!
-
Dhaval Parmar about 11 years@VipulShah: you are rock.can you tell me why this error come because before some time it work i have developed some tutotial ---> dj-android.blogspot.in/2013/02/… now same code is not working.
-
ericn about 11 yearsThis looks interesting but any references on why we need to do this workaround @Timmmm?
-
marimaf about 11 yearsI was adding the google play services as a Java build path library, but it needed to be added under Android/Library
-
Muzikant almost 11 yearsGoogle should really fix the basic example to use the
SupportMapFragment
. Doesn't work without it... -
AfroRick almost 11 yearsYou no longer need to add the Dummy file as Google Play Services Library has been updated with an "official dummy file" to support "some IDEs" (i.e. they made a fix for IntelliJ and Android Studio).
-
speedynomads almost 11 yearsThis fixed my issue. Copied the google-play-services_lib folder into my projects parent folder and then followed the instructions to set a module dependency. Thanks. The process is really unintuitive. Just about got the hang of it in eclipse before switching;
-
Brian White over 10 yearsPlay Services are now available on emulators running Android v4.2.2 or above.
-
user3560827 over 7 years@Vipul Shah: "Dont forget to add google-play-services.jar into your project." <--- this saved me, 11:58PM, I almost carried this problem into the next day, thank you!