mapFragment.getMapAsync(this) – NullPointerException

我正在使用com.google.android.gms:play-services-maps:7.5.0 Google地图服务的com.google.android.gms:play-services-maps:7.5.0版本。 当试图调用下面我得到java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { SupportMapFragment mapFragment = (SupportMapFragment) getActivity().getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); //error return inflater.inflate(R.layout.fragment_example_map, container, false); } 

fragment_example_map.xml文件:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="app.ExampleMap"> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:name="com.google.android.gms.maps.SupportMapFragment" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent"/> </FrameLayout> 

Solutions Collecting From Web of "mapFragment.getMapAsync(this) – NullPointerException"

你正试图find一个片段之前,它存在。 您指出具有该片段的布局是fragment_example_map.xml 。 但是,您正在试图在膨胀该布局文件之前查找地图片段。 这是行不通的。

除此之外,你似乎试图从另一个片段内部的片段的onCreateView()方法获取地图片段。 我不知道你为什么在这里嵌套片断,因为它似乎会使你的代码更复杂,更脆弱,没有明显的好处。

我在带有片段的地图上发表评论,首先你应该反思你的观点,因为你会从中调用一些东西,这就是为什么我首先夸大我的观点
View view = inflater.inflate(R.layout.activity_maps, null, false);

第二个调用子片段pipe理器this.getChildFragmentManager()而不是getActivity().getSupportFragmentManager()

以下是查看地图的完整示例

 import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; public class ClinicFragment extends Fragment implements OnMapReadyCallback { private GoogleMap mMap; public static ClinicFragment newInstance() { ClinicFragment fragment = new ClinicFragment(); return fragment; } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_maps, null, false); SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); return view; } /** * Manipulates the map once available. * This callback is triggered when the map is ready to be used. * This is where we can add markers or lines, add listeners or move the camera. In this case, * we just add a marker near Sydney, Australia. * If Google Play services is not installed on the device, the user will be prompted to install * it inside the SupportMapFragment. This method will only be triggered once the user has * installed Google Play services and returned to the app. */ @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; // Add a marker in Sydney and move the camera LatLng sydney = new LatLng(-34, 151); mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); } } 

需要许可

 <permission android:name="your.package.name.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-permission android:name="your.package.name.permission.MAPS_RECEIVE"/> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

加上function元素

 <uses-feature android:glEsVersion="0x00020000" android:required="true"/> 

大部分导入谷歌地图的关键

 <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your_key_here" /> 

更新添加这个元数据,如果你面临错误膨胀类的片段

 <meta-data android:name="com.google.android.geo.API_KEY" android:value="your_key_here" /> 

总结为mainfest

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="your.package.name" > // permission here <application android:name=".Activities.MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > // feature element // maps key </application> </manifest> 

activity_maps.xml

 <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.imaadv.leaynik.ClinicFragment" /> 

使用getChildFragmentManager()代替片段中的getActivity().getSupportFragmentManager()

喜欢:

 SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); 

如果你的片段中使用了android:name="com.google.android.gms.maps.MapFragment" ,那么改变你的代码:

 MapFragment mapFragment1 = (MapFragment)getFragmentManager().findFragmentById(R.id.map); mapFragment1.getMapAsync(this); 

但是,如果您的片段中使用了android:name="com.google.android.gms.maps.SupportMapFragment" ,请使用以下代码:

 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); 

根据Android版本或SDK版本5及以上的版本,使用getChildFragmentManager()及以下使用getFragmentManager()。

用这个。

 GoogleMap gooleMap; ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(GoogleMap map) { googleMap = map; } 

getFragmentManager()在使用片内映射时可能不起作用。 你必须使用getChildFragmentManager()

 SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.fragment_track_order_map_fragment); mapFragment.getMapAsync(this); 

下面是我的XML声明

 <fragment android:id="@+id/fragment_track_order_map_fragment" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> 

你也需要在你的片段中实现OnMapReadyCallback。

replace这个代码

 <fragment android:name="com.google.android.gms.maps.MapFragment" android:id="@+id/map" android:layout_width="fill_parent" android:layout_height="400dp" /> 

 if (googleMap == null) { mapFrag = (MapFragment)getFragmentManager().findFragmentById(R.id.map); mapFrag.getMapAsync(this); }else{ GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); } 

只需调用supportMapFragment.getMapAsync(this); 在你的活动onResume

简单的答案是如果你想添加Map Fragment这个

 <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:layout="@layout/fragment_home" /> 

在你的片段:像这样的布局

  <android.support.constraint.ConstraintLayout 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" tools:context="com.example.android.earthreport.fragments.HomeFragment" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="81dp"> <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="@id/guideline4" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@id/guideline3" tools:layout="@layout/fragment_home" /> </android.support.constraint.ConstraintLayout> 

然后,你必须添加这一行后,像这样的片段

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Find a reference to the {@link ListView} in the layout // Inflate the layout for this fragment // To get the referance we don't have findviewbyId // method in fragment so we use view View view = inflater.inflate(R.layout.fragment_home, container, false); SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager() .findFragmentById(R.id.map) mapFragment.getMapAsync(this); ..... 

进入你的活动:像这样的布局

 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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"> <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" 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" tools:context="package com.example.android.map" /> </android.support.constraint.ConstraintLayout> 

那么你必须添加这块代码。

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map_activity); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } 

纠正我,如果我错了,但总是添加地图片段后,膨胀布局或设置内容视图。