【问题标题】:Cannot create Cluster Manager instance( using Google Maps Android API Utility Library)无法创建集群管理器实例(使用 Google Maps Android API 实用程序库)
【发布时间】:2014-11-27 16:56:01
【问题描述】:

更新:(问题已解决) 最后它在我的 Intellij Idea 13

中工作

问题是库没有正确导入。 转到 Project Structure,选择 Modules,将库添加为来自现有源的模块, 然后点击它,它会显示Android并将其勾选为Library Module,然后它就可以工作了

我在我的应用程序中使用Google Maps Android API Utility Library。如果我创建一个 GoogleMap 实例并将相机移动到特定位置,它就可以工作!代码如下。

不幸的是,如果我调用 mClusterManager = new ClusterManager<MyItem>(this, mMap); 并向其中添加项目,应用程序将崩溃。任何人都可以帮忙吗?这是怎么回事?

public class MapActivity  extends  FragmentActivity{

    private GoogleMap mMap;
    private ClusterManager<MyItem> mClusterManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        setUpMapIfNeeded();
    }

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

    private void setUpMapIfNeeded() {
        if (mMap != null) {
            return;
        }
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        if (mMap != null) {
            //if comment out mCLusterManager instantiation, the app works and show the map 
            //mClusterManager = new ClusterManager<MyItem>(this, mMap);

           mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.5, -0.35), 9));
        }
    }
}

下面是map.xml(来自https://github.com/googlemaps/android-maps-utils

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:map="http://schemas.android.com/apk/res-auto"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>

logcat 中的错误:

10-02 18:13:08.667    2854-2854/your.androidclient E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: com.google.maps.android.clustering.ClusterManager
            at your.androidclient.AndroidClientActivity.setUpMapIfNeeded(AndroidClientActivity.java:115)
            at your.androidclient.AndroidClientActivity.onCreate(AndroidClientActivity.java:99)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 应用程序将崩溃 - 你希望我们猜到异常吗?
  • 我在我的手机上测试它,它只说“不幸的是应用程序已停止”,我尝试了`Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();`但没有得到任何东西
  • 你检查logcat的异常了吗?
  • @Luksprog 很抱歉我的回复迟了,我在过去 1 小时为我的 genymotion 设置了谷歌地图,但我在 logcat 中遇到了错误,请在问题区域查看。

标签: android google-maps android-maps-utils


【解决方案1】:

文档告诉你使用:

compile 'com.google.maps.android:android-maps-utils:0.4+'

使用它会报错。您必须使用 android-maps-utils:0.4.2

compile 'com.google.maps.android:android-maps-utils:0.4.2'

这将消除错误。

注意:我在另一个答案中发布了使用 0.3+,尽管 0.4.2 显然更好。无论哪种方式,0.4+ 都不起作用,而其他的则起作用

【讨论】:

  • 感谢似乎已经为我解决了。非常奇怪的修复! (我还被要求在引用 0.4+ 版本的库上使用 exclude)
【解决方案2】:

LogCat 状态

java.lang.NoClassDefFoundError: com.google.maps.android.clustering.ClusterManager

这意味着 maps-utils-library 不会添加到您的最终 APK 中。您是否将其添加为库(项目属性 > Android > 库)?

【讨论】:

  • 我正在使用intellij idea,我已经导入了库,但仍然无法使用
  • 嗯,我正在使用 Eclipse,所以很遗憾我无法帮助您使用 IntelliJ。但是如上所述,异常意味着最终 APK 中缺少该库。
  • 谢谢!为您 +1,在您的帮助下我会成功!
【解决方案3】:

使用以下方式编辑您的播放服务:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.google.android.gms:play-services-maps:9.2.1'
    compile 'com.google.android.gms:play-services-places:9.2.1'
    compile 'com.google.maps.android:android-maps-utils:0.4+'
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 2018-03-24
    • 2020-04-13
    • 1970-01-01
    相关资源
    最近更新 更多