使用map.clear()

时间:2018-10-17 13:33:42

标签: java android exception google-maps-markers

我在一个不可见的标记上有自定义infoWindow。 每次只有一个标记,因为一旦infoWindow关闭,该标记就会被删除。

无论如何都会发生以下情况:如果我打开infoWindow并单击工具栏上的一个选项项,该选项项会触发map.clear()调用,这将引发上述异常。

我已经读到我应该跟踪地图上的标记,因此我尝试存储对该标记的引用,并在调用map.clear()之前检查是否不为null。如果不是null,则标记将被删除,并且引用将更新为null,但仍然map.clear()调用仍然会产生异常。

MapsActivity

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.connection_type:

            // Creates an alert dialog where the user can choose which connectivity technology to monitor
            final CharSequence connectionsType[] = new CharSequence[]{"GSM", "UMTS", "LTE", "WIFI"};

            AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Theme_AppCompat_Dialog_Alert);
            builder.setTitle("Choose a wireless technology");
            builder.setItems(connectionsType, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    // Unregister active connectivity related receiver
                    unregisterProperReceiver(ACTIVE_CONNECTIVITY_TYPE);
                    if(marker != null) {
                        marker.remove();
                        marker = null;

                    }
                    // Clear map
                    mMap.clear();  /* devil is here */
                    ....



 // When the info windows  is closed delete the associated marker
    mMap.setOnInfoWindowCloseListener(new GoogleMap.OnInfoWindowCloseListener() {
        @Override
        public void onInfoWindowClose(Marker marker) {
            marker.remove();

        }
    });

任务(正是我设置标记参考的位置)

if (groupedAvgSignalStrength != null && groupedAvgSignalStrength.length != 0 )
        {

            String mgrsTen = groupedAvgSignalStrength[0].mgrsTen;
            String avgSignalStrength = String.valueOf(groupedAvgSignalStrength[0].signalStrength);
            String areaOccurrences = String.valueOf(groupedAvgSignalStrength[0].areaOccurrences);

            InfoWindowData info = new InfoWindowData(mgrsTen,areaOccurrences,avgSignalStrength);

            Marker position = mMap.addMarker(new MarkerOptions()
                    .position(location));

            position.setTag(info);

            // Make the marker invisible
            position.setAlpha(0);

            position.showInfoWindow();

            mapsActivity.setMarker(position); /* update marker ref*/

        }

堆栈跟踪

10-17 15:31:57.724 17953-17953/com.example.r.lamapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.r.lamapp, PID: 17953
    java.lang.IllegalArgumentException: Unmanaged descriptor
        at com.google.maps.api.android.lib6.common.n.d(:com.google.android.gms.dynamite_mapsdynamite@14366051@14.3.66 (040408-213742215):13)
        at com.google.maps.api.android.lib6.impl.q.c(:com.google.android.gms.dynamite_mapsdynamite@14366051@14.3.66 (040408-213742215):13)
        at com.google.maps.api.android.lib6.impl.dd.q(:com.google.android.gms.dynamite_mapsdynamite@14366051@14.3.66 (040408-213742215):106)
        at com.google.maps.api.android.lib6.impl.bf.e(:com.google.android.gms.dynamite_mapsdynamite@14366051@14.3.66 (040408-213742215):593)
        at com.google.android.gms.maps.internal.l.a(:com.google.android.gms.dynamite_mapsdynamite@14366051@14.3.66 (040408-213742215):301)
        at fq.onTransact(:com.google.android.gms.dynamite_mapsdynamite@14366051@14.3.66 (040408-213742215):4)
        at android.os.Binder.transact(Binder.java:499)
        at com.google.android.gms.internal.maps.zza.transactAndReadExceptionReturnVoid(Unknown Source)
        at com.google.android.gms.maps.internal.zzg.clear(Unknown Source)
        at com.google.android.gms.maps.GoogleMap.clear(Unknown Source)
            at com.example.r.lamapp.MapsActivity$4.onClick(MapsActivity.java:594)
            at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1068)
            at android.widget.AdapterView.performItemClick(AdapterView.java:310)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1155)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3133)
            at android.widget.AbsListView.onTouchUp(AbsListView.java:4060)
            at android.widget.AbsListView.onTouchEvent(AbsListView.java:3819)
            at android.view.View.dispatchTouchEvent(View.java:9941)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2663)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2344)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
            at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:411)
            at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1810)
            at android.app.Dialog.dispatchTouchEvent(Dialog.java:793)
            at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
            at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:373)
            at android.view.View.dispatchPointerEvent(View.java:10161)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4448)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4316)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3863)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3916)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3882)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4009)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3890)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4066)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3863)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3916)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3882)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3890)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3863)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6224)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6198)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6159)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6327)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
            at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
            at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
            at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6298)
            at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6350)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
            at android.view.Choreographer.doCallbacks(Choreographer.java:683)
            at android.view.Choreographer.doFrame(Choreographer.java:613)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
            at android.os.Handler.handleCallback(Handler.java:751)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:159)
            at android.app.ActivityThread.main(ActivityThread.java:6097)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

1 个答案:

答案 0 :(得分:0)

使用属于hideInfoWindow()类的Marker方法。 因此,当代码执行hideInfoWindow()时,它会触发onInfoWindowClose()回调,这反过来又会删除标记。

我认为该异常是由于以下事实引起的:我手动删除了标记(marker.remove()),并且还通过调用回调再次隐式删除了标记。

我仍然不明白为什么我在第二次marker.remove()调用时没有得到异常(对我来说似乎更合理)而不是在mMap.clear()处得到异常。

MapsActivity

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.connection_type:

            // Creates an alert dialog where the user can choose which connectivity technology to monitor
            final CharSequence connectionsType[] = new CharSequence[]{"GSM", "UMTS", "LTE", "WIFI"};

            AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Theme_AppCompat_Dialog_Alert);
            builder.setTitle("Choose a wireless technology");
            builder.setItems(connectionsType, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    // Unregister active connectivity related receiver
                    unregisterProperReceiver(ACTIVE_CONNECTIVITY_TYPE);
                    if(marker != null) {
                        marker.hideInfoWindow();
                        marker = null;
                    }
                    // Clear map
                    mMap.clear();  /* devil is here */
                    ....


 // When the info windows  is closed delete the associated marker
        mMap.setOnInfoWindowCloseListener(new GoogleMap.OnInfoWindowCloseListener() {
            @Override
            public void onInfoWindowClose(Marker marker) {
                marker.remove();

            }
        });