Error android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment

i am following tutorial for using google maps api v2 .i am facing an error error logs are

05-15 00:08:18.415: E/AndroidRuntime(4547): FATAL EXCEPTION: main
05-15 00:08:18.415: E/AndroidRuntime(4547): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.rtesh.onemore/org.rtesh.onemore.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.os.Looper.loop(Looper.java:150)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.ActivityThread.main(ActivityThread.java:4277)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at java.lang.reflect.Method.invokeNative(Native Method)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at java.lang.reflect.Method.invoke(Method.java:507)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at dalvik.system.NativeStart.main(Native Method)
05-15 00:08:18.415: E/AndroidRuntime(4547): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:250)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.Activity.setContentView(Activity.java:1712)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at org.rtesh.onemore.MainActivity.onCreate(MainActivity.java:27)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
05-15 00:08:18.415: E/AndroidRuntime(4547):     ... 11 more
05-15 00:08:18.415: E/AndroidRuntime(4547): Caused by: android.support.v4.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
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.support.v4.app.Fragment.instantiate(Fragment.java:395)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:264)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
05-15 00:08:18.415: E/AndroidRuntime(4547):     ... 20 more
05-15 00:08:18.415: E/AndroidRuntime(4547): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment in loader dalvik.system.PathClassLoader[/data/app/org.rtesh.onemore-1.apk]
05-15 00:08:18.415: E/AndroidRuntime(4547):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-15 00:08:18.415: E/AndroidRuntime(4547):     at android.support.v4.app.Fragment.instantiate(Fragment.java:385)
05-15 00:08:18.415: E/AndroidRuntime(4547):     ... 23 more

my MainActivity class is

@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity {
  static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private GoogleMap map;

@SuppressLint("NewApi")
@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();

    if (map!=null){
      Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
          .title("Hamburg"));
      Marker kiel = map.addMarker(new MarkerOptions()
          .position(KIEL)
          .title("Kiel")
          .snippet("Kiel is cool")
          .icon(BitmapDescriptorFactory
              .fromResource(R.drawable.ic_launcher)));
    }

  } 

}

and activity_main.xml is

<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"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout> 

i gave appropriate permissions in maifest class

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.rtesh.onemore"
    android:versionCode="1"
    android:versionName="1.0" >
<permission
        android:name="org.rtesh.android.locationapi.maps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
<uses-permission android:name="com.org.rtesh.android.locationapi.maps.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="org.rtesh.onemore.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="mykey" />

    </application>

</manifest>

i referenced the google play service library why i am facing this error ??please some one help and please point the major differences between google maps version1 and 2!!

Answers


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="match_parent"
       android:layout_height="match_parent" />

You need to use the SupportMapFragment for API under 11 !

In your MainActivity (or in each activity you want to put the v2 map) you must extend FragmentActivity or Activity either you want to use SupportMapFragment or just MapFragment. Use MapFragment class only if you are targeting API 12 and above. Otherwise, use SupportMapFragment. It's a subtle difference but it makes the error.

EDIT:

Please change your code in FragmentActivity:

map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map))
        .getMap();

Understand the concept. Since you are targeting lower android versions where fragments are not supported, you need to use a support library that takes care of your fragments. So wherever you are using fragments make sure you are importing a fragment from support library.


First, like the other responders to this post you should change the minimum Android SDK level to at least 11

android:minSdkVersion="11"

Here is a working example that I made yesterday.

activity_main.xml

<?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"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTargetLat="33.41662287712097"
map:cameraTargetLng="35.21843892856462"
map:cameraZoom="4"/>

</LinearLayout>In MainActivity.java onCreate()

In MainActivity.java onCreate()

public class MainActivity extends FragmentActivity  {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
GoogleMap mMap = mapFragment.getMap();

mMap.addMarker(new MarkerOptions()
.position(new LatLng(35.21843892856462, 33.41662287712097))
.title("My Office"));

}

To see my screenshot of the result of the code see my answer in this post. gl hf! Google Map V2 not working for me . Any sample?


public class SearchOffer extends FragmentActivity {

GoogleMap gmpSearchOfferMap;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        setContentView(R.layout.search_offers);

        if (gmpSearchOfferMap == null) {
            gmpSearchOfferMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.gmpOfferMap)).getMap();
        }

        if (gmpSearchOfferMap != null) {
            gmpSearchOfferMap.setMyLocationEnabled(true);
            gmpSearchOfferMap.getUiSettings().setMyLocationButtonEnabled(
                    true);
            gmpSearchOfferMap.getUiSettings().setZoomControlsEnabled(true);
            gmpSearchOfferMap.getUiSettings().setZoomGesturesEnabled(true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}


Your min sdk is 8. You will need to add support library for api versions below 12

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

So for backward compatibality you need add support library

android:name="com.google.android.gms.maps.SupportMapFragment "

Since you din't add support library you got classcastexception

To add support library check the link below

http://developer.android.com/tools/extras/support-library.html

For downloading the support fragment

http://developer.android.com/tools/extras/support-library.html#Downloading

For more information check the link

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapFragment

Check the line just above the heading developers guide.

For more information on map api v2 check the documentation

https://developers.google.com/maps/documentation/android/


Need Your Help

C++11 rvalue calls destructior twice

c++ multithreading c++11

I'm trying to make a class runner (run a class at a fixed time freq), which runs a class in another thread, and can be controlled (like pause, resume, stop) from main thread.

BlackBerry - Background bitmap doesn't fit for scrolling page

user-interface blackberry scroll background

I have a background bitmap in my Blackberry application screen. Screen has scrolling enabled as i must to have a scroll. The problem which i'm facing is, when i scroll down the page, background bit...