无法找到类com.google.android.gms.maps.SupportMapFragment

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法找到类com.google.android.gms.maps.SupportMapFragment相关的知识,希望对你有一定的参考价值。

所以我正在开发一款使用Google Maps v2 API来显示地图叠加层的应用。我知道之前已经问过这个问题,但我已经尝试了一切。我正在使用未植根且安装了Google Play服务的Nexus 5设备测试我的应用。

LogCat显示以下内容:

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

基本上,它说它找不到类com.google.android.gms.maps.SupportMapFragment。虽然我确信我在主项目中引用了Google Play服务。

下图显示我在项目中引用了Google Play服务:

另外,这是我的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>

最后,这是我的MainActivity.java(到目前为止,它已经从Google API演示中复制):

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);
    }
}

如果有人有任何想法,那就太好了!现在很困惑。

答案
  • 我没有看到你提到添加Android支持库android-support-v4.jar。您需要确保Eclipse正确包含它才能使com.google.android.gms.maps.SupportMapFragment工作:
  • 另一个潜在问题是您的应用中的播放服务库与您的nexus 5设备上的播放服务版本不匹配。如果您从SDK管理器下载了最新的库,则其版本可能高于设备上的库。我猜你的设备上的播放服务版本是5.0.89,你可以查看设备Settings -> Apps -> Downloaded -> Google Play Services。如果是这种情况,请尝试使用旧版本的库。

另一答案

你有没有尝试过这个建议:Unable instantiate android.gms.maps.MapFragment

“尝试在Eclipse中右键单击您的项目,选择Android工具>添加支持库。在我的例子中,它是从ActionBarSherlock获取的旧版本。”

另一答案

导入Google Play service-lib时,您是否已将其添加到工作区中。尝试将其添加到工作区

另一答案
  1. 您是否曾确保google-play-services_lib与您当前的项目位于同一目录中?
  2. 确保添加“android-support-v4.jar”。
  3. 没看到你显示布局文件。确保添加“element.enter image description here

以上是关于无法找到类com.google.android.gms.maps.SupportMapFragment的主要内容,如果未能解决你的问题,请参考以下文章

谷歌地图修订版。 30 日食用法:google_play_services_version

42AppCompatActivity类无法找到

无法从类加载器中找到 QueryDsl JPAQueryFactory

无法在类路径中找到组合

反应组件无法找到我的 CSS 类

无法在 intellij idea 类路径中找到属性文件