Unable instantiate android.gms.maps.MapFragment

2019-01-01 10:55发布

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?

30条回答
忆尘夕之涩
2楼-- · 2019-01-01 11:15

I got the same problem and just Installed Play Services from SDK and all problems fly away.

查看更多
十年一品温如言
3楼-- · 2019-01-01 11:17

First Step http://developer.android.com/tools/projects/projects-eclipse.html#ReferencingLibraryProject Second Step http://developer.android.com/google/play-services/setup.html On that page it has a NOTE but we didn't pay attention

Note: You should be referencing a copy of the library that you copied to your source tree—you should not reference the library from the Android SDK directory.

But I think it's not a good way of documentation :)

查看更多
无色无味的生活
4楼-- · 2019-01-01 11:18

As Google map is wrapped under Fragments now, extend your activity from FragmetActivity not just Activity and make sure if you use "supporrtMapFragment" then you are importing Fragment from support package only.

查看更多
宁负流年不负卿
5楼-- · 2019-01-01 11:19

I faced the same problem ant it took me tow days to figure out a solution that worked for me :

  1. Delete the project google-play-services_lib (right click on the project delete )
  2. Delete the project containing the Google maps demo ( MainActivity in my case ) if you have one
  3. 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 )
  4. 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 :

notice the

Note : You should not have something like this ( the project should be referred from your workspace ):

enter image description here

I think that the problem is that tow projects are referencing the same library

查看更多
骚的不知所云
6楼-- · 2019-01-01 11:19

It was a little difficult because it was different in the previous API, but I found a solution. Google says what to do here; according with the question we need com.google.android.gms classes so it is necessary to setup the google play services, which is just a library that we have to add to our project like this link . It's very important to import the copy of the project library google-play-services_lib not the one that is in the sdk folder. Once that's done, the tutorial from Google goes perfectly.

查看更多
低头抚发
7楼-- · 2019-01-01 11:19

This might be of help to some. I had two projects, one which was a copy of the demo from Google and that worked fine. Another I copied into an existing project and I could not get it to run at all. And in the second failing one I was getting the error message described above.

My problem was due to a library not enabled, even though I rebuilt, imported multiple times etc. Right-click on the project -> Properties -> Java Build Path -> Order & Export tab. On the failing project the "Android Private Libraries" tab was unchecked.

Once I enabled it the project worked fine.

查看更多
登录 后发表回答