Google Maps:具有AutoCompleteActivity的SupportMapFragment导致onResume出现NullPointerException

问题描述

我有一个包含SupportMapFragment和BottomSheet的活动 (对于与用户地址有关的文本字段),地图占用了大部分 空格)。图片如下:

enter image description here

单击“更改位置”时,此功能称为:


    private fun startAutoCompleteActivity()
                {
                    val fields = Arrays.asList(
                        Place.Field.ID,Place.Field.NAME,Place.Field.LAT_LNG,Place.Field.ADDRESS,Place.Field.ADDRESS_COMPONENTS
                    )
                    // Start the autocomplete intent.
                    val intent = Autocomplete.IntentBuilder(
                        AutocompleteActivityMode.OVERLAY,fields
                    )
                        .setCountry("IN")
                        .build(this)
                    //.setTypeFilter(TypeFilter.REGIONS)
                    startActivityForResult(
                        intent,PLACE_SEARCH
                    )
               }

在“自动完成活动”中搜索某些内容并选择建议后,恢复活动后会发生以下崩溃(选择建议后,我们将地图指针移至所选建议的经度,并通过反向地理编码器获取地址

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method       'com.google.maps.api.android.lib6.impl.model.n     com.google.maps.api.android.lib6.impl.model.o.a()' on a null object reference
       at com.google.maps.api.android.lib6.gmm6.vector.label.g.a(g.java:72)
       at com.google.maps.api.android.lib6.gmm6.vector.label.g.d(g.java:7)
       at com.google.maps.api.android.lib6.gmm6.vector.label.g.a(g.java:52)
       at com.google.maps.api.android.lib6.gmm6.vector.bt.a(bt.java:126)
       at com.google.maps.api.android.lib6.gmm6.vector.av.run(av.java:54)

混淆了堆栈跟踪,我已经在Google Issue Tracker上报告了一个问题,但是仍然没有解决https://issuetracker.google.com/issues/158844550

activity_add_new_address布局:


    <?xml version="1.0" encoding="utf-8"?>
    
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".ui.address.AddNewAddress">
    
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayoutContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/mapContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="220dip"
                app:layout_constraintTop_toBottomOf="parent">
    
                <androidx.fragment.app.FragmentContainerView
                    android:id="@+id/mapLayoutDelivery"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
    
                    />
    
                <androidx.appcompat.widget.AppCompatimageView
                    android:id="@+id/marker"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:contentDescription="@null"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@drawable/ic_address_gradient" />
    
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    
        <androidx.cardview.widget.CardView
            android:id="@+id/bottom_sheet"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:tag="binding_6"
            app:behavior_hideable="false"
            app:behavior_peekHeight="220dp"
            app:cardElevation="5.0dip"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
    
            <include
                layout="@layout/content_add_new_address"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:behavior_hideable="true"
                app:layout_behavior="@string/bottom_sheet_behavior" />
        </androidx.cardview.widget.CardView>
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

我已经在onCreate中初始化了这样的地图:

 

      private fun initializeMap() {
            mapSupport = SupportMapFragment
                .newInstance(
                    GoogleMapOptions().camera(
                        CameraPosition(
                            LatLng(20.5937,78.9629),4f,30f,0f
                        )
                    )
                )
    
            supportFragmentManager.beginTransaction().replace(R.id.mapLayoutDelivery,mapSupport)
                .commit()
            checkLocationServiceAndGetMapAsync()
        }

用户从地图移动到“自动完成”活动,然后又回到“地图”活动时,应用程序崩溃。崩溃并非发生在所有设备上,仅发生在发行版中而不是调试中,发生崩溃的设备并非每次都发生。有很多随机性,请帮帮我!!。

onResume中的代码


    override fun onResume() {
            super.onResume()
                if (settingsClicked) {
                    settingsClicked = false
                    if (PermissionHelper.locationPermissionGranted()) {
        
                        if (PermissionHelper.checkLocationSettings()) {
                            requestAndUpdateLocation()
                        } else {
                            displayLocationSettingsRequest()
                        }
                    } else {
                        locationChecks()
                    }
                }
                val animate = false
                if (::bottomSheetBehavior.isInitialized)
                    if (bottomSheetBehavior.state == BottomSheetBehavior.STATE_COLLAPSED) {
                        setMapMarginBotttom(Math.round(dpToPx(220)),animate);
                    } else {
                        setMapMarginBotttom(bottom_sheet.height,animate);
                    }
        } 

还已经尝试了以下解决方案: android:hardwareAccelerated = true,minMaxZoomPreference最多只能设置16个

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...