Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

java - Could Not Find Class com.google.android.gms.maps.SupportMapFragment


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.

See Question&Answers more detail:os</a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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."


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...