简体   繁体   English

Android-Maps-Extensions InfoContents产生例外

[英]Android-Maps-Extensions InfoContents Generating Exception

In part of my method, I configure infocontents to get html content from the marker's "title" and display this in the infowindow. 在我的方法的一部分中,我配置infocontents以从标记的“标题”获取html内容,并将其显示在infowindow中。 I also make the window a clickable link. 我还使窗口成为可单击的链接。 I am using Android-Maps-Extensions to cluster the markers. 我正在使用Android-Maps-Extensions对标记进行聚类。

private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getExtendedMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                    @Override
                    public View getInfoWindow(Marker marker) {
                        return null;
                    }

                    @Override
                    public View getInfoContents(Marker marker) {
                        View v = getLayoutInflater().inflate(R.layout.infowindow_layout, null);
                        TextView textView = (TextView) v.findViewById(R.id.marker_label);
                        Spanned spannedContent = Html.fromHtml(marker.getTitle());
                        textView.setText(spannedContent, TextView.BufferType.SPANNABLE);
                        return v;
                    }
                });

                mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                    @Override
                    public void onInfoWindowClick(Marker marker) {
                        int firstindex = marker.getTitle().indexOf("href=")+5;
                        int lastindex = marker.getTitle().indexOf("target='_blank'");
                        String s = marker.getTitle().substring(firstindex,lastindex);
                        Log.d("myapplication", s);
                        Uri uri = Uri.parse(s);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);

                    }
                });

                ClusteringSettings clusteringSettings = new ClusteringSettings();
                mMap.setClustering(clusteringSettings);

                setUpMap();
            }
        }
    }

When I press on an individual marker, it shows the infowindow just fine (like before I added the ClusterSettings and setClustering code). 当我按一个单独的标记时,它显示的信息窗口很好(就像在添加ClusterSettings和setClustering代码之前一样)。 However, when I press on a cluster, I get this exception: 但是,当我按集群时,会出现以下异常:

01-08 15:00:20.210    3384-3384/com.example.ron.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at java.io.StringReader.<init>(StringReader.java:46)
            at android.text.HtmlToSpannedConverter.convert(Html.java:411)
            at android.text.Html.fromHtml(Html.java:139)
            at android.text.Html.fromHtml(Html.java:102)
            at com.example.ron.myapplication.MapsActivity$1.getInfoContents(MapsActivity.java:81)
            at com.androidmapsextensions.impl.DelegatingGoogleMap$DelegatingInfoWindowAdapter.getInfoContents(DelegatingGoogleMap.java:414)
            at com.google.android.gms.maps.GoogleMap$13.g(Unknown Source)
            at com.google.android.gms.maps.internal.d$a.onTransact(Unknown Source)
            at android.os.Binder.transact(Binder.java:279)
            at com.google.android.gms.maps.internal.p.b(SourceFile:112)
            at com.google.maps.api.android.lib6.c.h.a(Unknown Source)
            at com.google.maps.api.android.lib6.c.h.a(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.c.h.b(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.c.j.c(Unknown Source)
            at com.google.maps.api.android.lib6.c.al.c(Unknown Source)
            at com.google.maps.api.android.lib6.c.aj.g(Unknown Source)
            at com.google.maps.api.android.lib6.c.al.h(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.c.h.a(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.o.aw.a(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.o.bf.a(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.o.be.a(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.o.bx.d(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.o.ak.onSingleTapConfirmed(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.i.g.onSingleTapConfirmed(Unknown Source)
            at com.google.maps.api.android.lib6.gmm6.i.i.handleMessage(Unknown Source)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:3687)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
            at dalvik.system.NativeStart.main(Native Method)

It seems like when I press on the cluster, it calls on the infocontent and infowindow. 好像当我按集群时,它会调用infocontent和infowindow。 Is there anyway I can disable the infowindow for the actual clusters and just have it zoom in when the cluster is pressed? 无论如何,我可以禁用实际群集的信息窗口,而仅在按下群集时才放大它吗?

Please comment below if you need more clarification or if you would like more information. 如果您需要更多说明或需要更多信息,请在下面发表评论。

Edit: Solved thanks to MaciejGórski. 编辑:感谢MaciejGórski解决。 I changed getInfoContents method to this so that I would not get an error: 我将getInfoContents方法更改为此,这样我就不会出现错误:

public View getInfoContents(Marker marker) {
                    View v = getLayoutInflater().inflate(R.layout.infowindow_layout, null);
                    if (marker.isCluster()) {
                        return null;
                    }
                    TextView textView = (TextView) v.findViewById(R.id.marker_label);
                    Spanned spannedContent = Html.fromHtml(marker.getTitle());
                    textView.setText(spannedContent, TextView.BufferType.SPANNABLE);
                    return v;
                }
            });

Cluster markers have no title by default. 默认情况下,群集标记没有标题。 You are getting NPE, because you send null to Android code that expects some non-null value. 之所以会获得NPE,是因为将空值发送给需要一些非空值的Android代码。

You may want to generate title for them in a different way, eg by iterating over all markers in a cluster: 您可能想以不同的方式为它们生成标题,例如,通过遍历集群中的所有标记:

if (marker.isCluster()) {
    Collection<Marker> markers = marker.getMarkers();
    // itarate over markers and getTitle on each
} else {
    // your old code
}

See relevant code in demo app: DemoFragment.java . 请参阅演示应用程序中的相关代码: DemoFragment.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM