1


So I'm working on an app that uses Google Maps v2 API to display a map overlay. I know this question has been asked before but I've tried everything. I am testing my app using a Nexus 5 device that is not rooted and has Google Play Services installed.

LogCat displays the following:

09-14 20:18:27.646: E/dalvikvm(10951): Could not find class 'com.google.android.gms.maps.SupportMapFragment', referenced from method com.bryansworld.algonquinreview.MainActivity.setUpMapInitial
09-14 20:18:27.686: E/AndroidRuntime(10951): FATAL EXCEPTION: main
09-14 20:18:27.686: E/AndroidRuntime(10951): Process: com.bryansworld.algonquinreview, PID: 10951
09-14 20:18:27.686: E/AndroidRuntime(10951): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bryansworld.algonquinreview/com.bryansworld.algonquinreview.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.os.Handler.dispatchMessage(Handler.java:102)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.os.Looper.loop(Looper.java:136)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.ActivityThread.main(ActivityThread.java:5001)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at java.lang.reflect.Method.invokeNative(Native Method)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at java.lang.reflect.Method.invoke(Method.java:515)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at dalvik.system.NativeStart.main(Native Method)
09-14 20:18:27.686: E/AndroidRuntime(10951): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.Activity.setContentView(Activity.java:1929)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at com.bryansworld.algonquinreview.MainActivity.onCreate(MainActivity.java:33)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.Activity.performCreate(Activity.java:5231)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
09-14 20:18:27.686: E/AndroidRuntime(10951):    ... 11 more
09-14 20:18:27.686: E/AndroidRuntime(10951): 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
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.Fragment.instantiate(Fragment.java:597)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.Fragment.instantiate(Fragment.java:561)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.Activity.onCreateView(Activity.java:4778)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
09-14 20:18:27.686: E/AndroidRuntime(10951):    ... 21 more
09-14 20:18:27.686: E/AndroidRuntime(10951): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.SupportMapFragment" on path: DexPathList[[zip file "/data/app/com.bryansworld.algonquinreview-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.bryansworld.algonquinreview-1, /vendor/lib, /system/lib]]
09-14 20:18:27.686: E/AndroidRuntime(10951):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
09-14 20:18:27.686: E/AndroidRuntime(10951):    at android.app.Fragment.instantiate(Fragment.java:583)
09-14 20:18:27.686: E/AndroidRuntime(10951):    ... 24 more

Basically, it says that it cannot find class com.google.android.gms.maps.SupportMapFragment. Although I am sure I referenced Google Play Services in my main project.



The following image shows that I referenced Google Play Services in my project: Showing that I referenced Google Play Services



Also, here is my ApplicationManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bryansworld.algonquinreview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <!-- Permissions for operation  -->
    <permission android:name="com.bryansworld.algonquinreview.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
    <uses-permission android:name="com.bryansworld.algonquinreview.package.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" />

    <!-- Required OpenGL ES 2.0. for Maps V2 -->
    <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=".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>

        <!-- Google API Key -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="KEY_GOES_HERE" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

    </application>

</manifest>



Finally, here is my MainActivity.java (So far, it has been copied from the Google API demo):

package com.bryansworld.algonquinreview;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.TileOverlay;
import com.google.android.gms.maps.model.TileOverlayOptions;
import com.google.android.gms.maps.model.TileProvider;
import com.google.android.gms.maps.model.UrlTileProvider;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;

public class MainActivity extends FragmentActivity {
    //The Algonquin Map URL
    private static final String ALGONQUIN_URL_FORMAT =
            "http://www.muscedere.ca/algonquin_map/%d/%d/%d.png";

    //Map Elements
    private GoogleMap mMap;
    private TileOverlay mTiles;

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

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapInitial();
    }

    private void setUpMapInitial(){
        //Check to ensure map isn't already set up.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();

            //Now, we see if we are successful.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.setMapType(GoogleMap.MAP_TYPE_NONE);

        TileProvider tileProvider = new UrlTileProvider(256, 256) {
            @Override
            public synchronized URL getTileUrl(int x, int y, int zoom) {
                // The moon tile coordinate system is reversed.  This is not normal.
                int reversedY = (1 << zoom) - y - 1;
                String s = String.format(Locale.US, ALGONQUIN_URL_FORMAT, zoom, x, reversedY);
                URL url = null;
                try {
                    url = new URL(s);
                } catch (MalformedURLException e) {
                    throw new AssertionError(e);
                }
                return url;
            }
        };

        mTiles = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
    }

    public void setFadeIn(View v) {
        if (mTiles == null) {
            return;
        }
        mTiles.setFadeIn(((CheckBox) v).isChecked());
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}



If anyone has any idea, that would be great! Pretty confused right now.

Bryan Muscedere
  • 572
  • 2
  • 10
  • 23
  • 1
    Nothing leaps out at me as the source of your problem. However, why are you using `SupportMapFragment` and `FragmentActivity` anyway? Your `minSdkVersion` is 14, which means you could use native fragments instead of the backport. – CommonsWare Sep 15 '14 at 00:41
  • That is true. Didn't realize that! I switched my code from `SupportMapFragment` to `MapFragment` but now I just get a `com.google.android.gms.maps.MapFragment` error instead. – Bryan Muscedere Sep 15 '14 at 00:44
  • 1
    Did you do anything to your project for adding in the Play Services SDK other than adding it to the Android section of your project settings (per the screenshot)? For example, did you also manually fuss with your build path? If so, undo that and see if it helps. – CommonsWare Sep 15 '14 at 00:45
  • Nope. I just added it to my project via the Properties screen shown above. – Bryan Muscedere Sep 15 '14 at 00:48
  • Can you post your layout xml? Also, in your manifest `android:value="KEY_GOES_HERE"` you have added the key you got from the console, right? – transporter_room_3 Sep 20 '14 at 09:33
  • Try using `android:name="com.google.android.gms.maps.SupportMapFragment"` instead of the `class=...` attribute – transporter_room_3 Sep 20 '14 at 09:39

4 Answers4

0

Have you tried this suggestion: Unable instantiate android.gms.maps.MapFragment?

"Try right-clicking on your project in Eclipse, select Android Tools > Add Support Library. In my case it was a stale version taken from ActionBarSherlock."

Community
  • 1
  • 1
Sasa Sekulic
  • 669
  • 4
  • 12
  • I followed that suggestion fully and it still didn't seem to work. – Bryan Muscedere Sep 15 '14 at 01:03
  • I'm sorry, haven't used Eclipse in almost a year and I don't have it installed to try every remedy, but did you do it like here: https://blog-emildesign.rhcloud.com/?p=435? It says the support library should be added and maybe also check the permissions. – Sasa Sekulic Sep 15 '14 at 15:50
0
  • I do not see you mention adding the Android support library android-support-v4.jar. You need to make sure Eclipse is properly including it for com.google.android.gms.maps.SupportMapFragment to work:

    Eclipse Android project included libraries path

  • Another potential issue is mismatch of play services library in your app and the play services verion on your nexus 5 device. If you downloaded the latest library from SDK manager, it may be higher version than the one on your device. I am guessing your play services version on device is 5.0.89, you can check on device Settings -> Apps -> Downloaded -> Google Play Services. Try using older version of the library if that's the case.

Finding Google Play Services version

ashoke
  • 6,441
  • 2
  • 26
  • 25
0

When importing the Google play service-lib, did you add it into the work space. try adding it in to the workspace

0
  1. Have you ever make sure that the google-play-services_lib is in the same directory with your current project?
  2. Make sure the "android-support-v4.jar" is added.
  3. Did not see you show the layout file. Make sure that you add " element. enter image description here
chengji18
  • 943
  • 1
  • 8
  • 9