Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 空指针异常mapview.onResume_Android_Google Maps_Androidx - Fatal编程技术网

Android 空指针异常mapview.onResume

Android 空指针异常mapview.onResume,android,google-maps,androidx,Android,Google Maps,Androidx,我有以下空指针异常 这段代码可以使用,但我将GMS库升级为androidx库 binding.mapView.onResume() 任何人都能提供有关正在发生的事情的线索吗?以下是您需要检查的事项: 确保这两个都有(我只有api密钥元数据) 现在,如果您使用的是fragment,请尝试以下操作,-->而不是在onCreateView中获取地图,而是在onViewCreated中获取地图,如下所示: @Override public void onViewCreated(View

我有以下空指针异常

这段代码可以使用,但我将GMS库升级为androidx库

binding.mapView.onResume()

任何人都能提供有关正在发生的事情的线索吗?

以下是您需要检查的事项:

  • 确保这两个都有(我只有api密钥元数据)

    现在,如果您使用的是fragment,请尝试以下操作,-->而不是在onCreateView中获取地图,而是在onViewCreated中获取地图,如下所示:

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.rewards_google_map_area);
        if (mapFragment != null) {
            LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
            if (locationManager != null && mapFragment.getMap() != null) {
                googleMap=mapFragment.getMap();
                //after this you do whatever you want with the map
            }
        }
    
     }
    
    看看这个答案,它涉及到关于androidx和map片段不兼容的bug跟踪器。可能和gradle属性提到的一样简单。
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    
     <fragment
       android:id="@+id/google_map_fragment"
       class="com.google.android.gms.maps.SupportMapFragment"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>
    
    public class ParentFragment extends Fragment {
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.rewards_google_map_area);
        if (mapFragment != null) {
            LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
            if (locationManager != null && mapFragment.getMap() != null) {
                googleMap=mapFragment.getMap();
                //after this you do whatever you want with the map
            }
        }
    
     }