4

So I was following the Google maps API v2 for android, trying to get a basic map on screen. After following all the steps, I get an "Error inflating class fragment" Other people have run into the same problem but none of the solutions seem to work. Exact error : android.view.InflateException: Binary XML file line #10: Error inflating class fragment

.java:

import android.os.Bundle;
import android.view.Menu;
import android.support.v4.app.FragmentActivity;


public class GoogleMaps extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_maps);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.google_maps, menu);
        return true;
    }

}

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".GoogleMaps" >
    <fragment 
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:name="com.testing.svma.MainActivity"/>
</RelativeLayout>

I think I have set up the Google play services correctly, it asks to download them when the page is navigated to for the first time. Here is my project.properties:

# Project target.
target=android-17
android.library=false
android.library.reference.1=../../../android-sdks/extras/google/google_play_services/libproject/google-play-services_lib

log cat:

...
03-06 21:12:44.751: I/System.out(9122): Debugger has connected
03-06 21:12:44.751: I/System.out(9122): waiting for debugger to settle...
03-06 21:12:46.166: I/System.out(9122): debugger has settled (1487)
03-06 21:12:50.634: D/dalvikvm(9122): DexOpt: couldn't find field Landroid/content/res/Configuration;.smallestScreenWidthDp
03-06 21:12:50.634: W/dalvikvm(9122): VFY: unable to resolve instance field 23
03-06 21:12:50.634: D/dalvikvm(9122): VFY: replacing opcode 0x52 at 0x0012
03-06 21:12:50.634: D/dalvikvm(9122): VFY: dead code 0x0014-0018 in Lcom/google/android/gms/common/GooglePlayServicesUtil;.b (Landroid/content/res/Resources;)Z
03-06 21:12:51.126: W/dalvikvm(9122): Unable to resolve superclass of Lmaps/p/s; (427)
03-06 21:12:51.126: W/dalvikvm(9122): Link of class 'Lmaps/p/s;' failed
03-06 21:12:51.126: W/dalvikvm(9122): Unable to resolve superclass of Lmaps/y/bo; (3820)
03-06 21:12:51.126: W/dalvikvm(9122): Link of class 'Lmaps/y/bo;' failed
03-06 21:12:51.126: W/dalvikvm(9122): Unable to resolve superclass of Lmaps/i/k; (4208)
03-06 21:12:51.126: W/dalvikvm(9122): Link of class 'Lmaps/i/k;' failed
03-06 21:12:51.126: E/dalvikvm(9122): Could not find class 'maps.i.k', referenced from method maps.z.ag.a
03-06 21:12:51.126: W/dalvikvm(9122): VFY: unable to resolve new-instance 3540 (Lmaps/i/k;) in Lmaps/z/ag;
03-06 21:12:51.126: D/dalvikvm(9122): VFY: replacing opcode 0x22 at 0x006d
03-06 21:12:51.149: D/dalvikvm(9122): VFY: dead code 0x006f-007f in Lmaps/z/ag;.a (Landroid/view/LayoutInflater;Lcom/google/android/gms/maps/GoogleMapOptions;ZLjava/lang/String;)Lmaps/z/ag;

I have tried solutions from these threads with no luck:

Error inflating class fragment

Google Maps V2 - Error inflating class Fragment

I also just followed this tutorial and got the same error:

https://www.youtube.com/watch?v=OZRgiwGfvSQ

Community
  • 1
  • 1
that_guy
  • 2,313
  • 4
  • 33
  • 46
  • Same here! Did you get this to work? – Stéphane Bruckert Mar 14 '13 at 07:51
  • Yes, not sure how though, did a few things... did you copy the Google play services lib into the project directory rather than referencing it in the sdk directory? – that_guy Mar 14 '13 at 19:38
  • I have this example working with googlemaps v2 with an Activity using a FragmentActivity for lower OS and higher with the latest Android-support library. It was difficult but its possible. look at this example: http://android-er.blogspot.com/2012/12/using-supportmapfragment.html – papachan Aug 27 '13 at 17:06

5 Answers5

1

This problem is usually derived from the fact that your project lacks the android-support library. This happens because in old versions of api fragment weren't exist so you need to add this library in order to add support for fragments in older version of Android.

To do this right click on your project and then:

enter image description here

In addition you can go over this blog post I wrote on implementing Google Maps in your application:

Google Maps API V2

Emil Adz
  • 40,709
  • 36
  • 140
  • 187
1

I had the same issue and tried each possiblty like 2 days but i changed my layout xml file like that and in java code i used supportmapfragment

also import android support library from package explorer

 <?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:name="com.google.android.gms.maps.SupportMapFragment"

        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>
1

I had fixed this problem by adding these lines in manifest.

<meta-data android:name="com.google.android.gms.version"
       android:value="@integer/google_play_services_version"/>
Evan
  • 1,152
  • 1
  • 11
  • 18
0

Follow all the steps in this link

You have missed to reference the Google maps add this in your code

public class GoogleMaps extends FragmentActivity {
    private GoogleMap mMap;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_maps);
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.google_maps, menu);
        return true;
    }

}

Other than add map key , add play service to project

Basavaraj Hampali
  • 3,905
  • 1
  • 19
  • 22
0

I had the same error and manage to solve it with multiple tries from difference stackoverflow answers:

Not sure which is/are the one that help to solve the problem, but I shall just list the things i had done:

  • Right Click on Project -> Properties -> Android -> Change Project Build Target to Android 4.1.2 (Android Open Source Project) -> Under Library, Remove the google-play-service-lib and Add it back -> Click "Apply" -> follow by "OK"
  • Right Click on Project -> Android Tools -> Fix Project Properties (by Emil Adz)
  • Reference the Google maps in class (by Basavaraj Hampali)

    private GoogleMap mMap;
    
    mMap = ((SupportMapFragment) this.getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    if (mMap== null) {
        Toast.makeText(this,"Map not Available", Toast.LENGTH_LONG).show();
    }
    
  • extends android.support.v4.app.FragmentActivity instead of just "FragmentActivity" (by ayush1794)

    extends android.support.v4.app.FragmentActivity
    

Hope that it helps :)

Community
  • 1
  • 1
Lee Yi Hong
  • 1,310
  • 13
  • 17