mapFragment.getMapAsync(this) - NullPointerException
Posted
技术标签:
【中文标题】mapFragment.getMapAsync(this) - NullPointerException【英文标题】: 【发布时间】:2015-09-08 13:29:12 【问题描述】:我正在使用com.google.android.gms:play-services-maps:7.5.0
版本的 Google 地图服务。尝试拨打以下电话时,我得到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_
android:layout_
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_
android:layout_/>
</FrameLayout>
【问题讨论】:
【参考方案1】:我在带有片段的地图中的评论,首先你应该引用你的观点,因为你会从中调用一些东西,这就是为什么我首先夸大我的观点View view = inflater.inflate(R.layout.activity_maps, null, false);
第二次调用子片段管理器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" />
加特征元素
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
最重要的谷歌地图键
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_key_here" />
更新如果您遇到Error inflating class fragment,请添加此元数据
<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_
android:layout_
tools:context="com.imaadv.leaynik.ClinicFragment" />
【讨论】:
@Fawzy,我没有看到“getChildFragmentManager” 请注意getChildFragmentManager
需要API 17或以上
如果您使用了来自 support-v4 或 support-v13 的 Fragment,请阅读此问题***.com/questions/25595388/…【参考方案2】:
使用getChildFragmentManager()
代替片段中的getActivity().getSupportFragmentManager()
。
喜欢:
SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
【讨论】:
【参考方案3】:您正试图在片段存在之前找到它。您指出具有片段的布局是fragment_example_map.xml
。但是,您试图在扩展该布局文件之前找到地图片段。这行不通。
除此之外,您似乎正试图从另一个片段内部获取地图片段,在该片段的onCreateView()
方法中。我不知道你为什么要在这里嵌套片段,因为它似乎会使你的代码更复杂、更脆弱而没有明显的好处。
【讨论】:
那么在哪里可以调用getMapAsync()
?基本上我的应用程序有 3 个片段、1 个活动和一个 ViewPager 在这些片段之间滑动。应该是活动的onCreate()
吗?
@Neutrino:“基本上我的应用程序有 3 个片段,1 个活动和一个 ViewPager 在这些片段之间滑动”——您不需要嵌套片段。您的活动有一个ViewPager
。您的 PagerAdapter
创建了三个片段。不需要嵌套。让FragmentPagerAdapter
直接创建SupportMapFragment
的新实例;您不需要膨胀布局。此示例显示了一个带有十个地图片段的ViewPager
:github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Pager
对不起,我有点迷路了。我的ViewPagerAdapter
的getItem()
检查位置并在这种情况下调用return new Example()
- 我猜在这种情况下需要膨胀布局?然后我不太确定我应该在哪里进行操作:在片段代码、活动代码或其他任何地方。
@Neutrino:我在之前的评论中提供了一个示例应用程序的链接,该链接演示了如何使用ViewPager
设置活动,并使用地图(在我的情况下为 10 个地图)作为通过片段的页面。您可能希望查看此示例应用程序,看看我是如何做到的。
删除了onCreateView
,类现在扩展了SupportMapFragment
,添加了onActivityCreated
,它现在似乎可以工作了,谢谢【参考方案4】:
如果您在片段中使用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);
【讨论】:
这个答案描述得很好,对我有用【参考方案5】:面临同样的问题。由于您在 Fragment 中使用 Map 片段,因此请使用 getChildFragmentManager()
而不是 getActivity().getFragmentManager()
或 getFragmentManager()
。
【讨论】:
【参考方案6】:这对我有用:
SupportMapFragment mapFragment = (SupportMapFragment)getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
【讨论】:
【参考方案7】:简单的答案是如果你想添加 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_
android:layout_
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_
android:layout_
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_
android:layout_
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_
android:layout_>
<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_
android:layout_
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);
如果我错了,请纠正我,但总是在填充布局或设置内容视图后添加地图片段。
【讨论】:
【参考方案8】:我使用的是 SupportMapFragment 并在 XML 中定义了 MapFragment 名称属性
android:name="com.google.android.gms.maps.MapFragment"
所以我用 SupportMapFragment 替换了代码,它开始正常工作
android:name="com.google.android.gms.maps.SupportMapFragment"
【讨论】:
【参考方案9】:如果你想在侧片段中使用地图片段,请使用这个
SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.flMap, mMapFragment);
fragmentTransaction.commit();
mMapFragment.getMapAsync(this);
【讨论】:
【参考方案10】:根据 Android 版本或 SDK 版本 5 及更高版本使用 getChildFragmentManager(),低于它使用 getFragmentManager()。
【讨论】:
您有相关文件或链接吗?在我的例子中,getChildFragmentManager() 为所有 Android 版本返回一个有效的 FragmentManager。但是对于 Android 4 及更低版本,getFragmentManager 返回 null。 您能否提供文档。这大致是我的问题的解决方案,但在我确定这些版本号之前,我无法将其投入生产。我读到的所有内容都是指 API 17 及更高版本。【参考方案11】: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_
android:layout_ />
您还需要在片段中实现 OnMapReadyCallback。
【讨论】:
【参考方案12】:用它代替 SupportMapFragment:)
GoogleMap gooleMap;
((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback()
@Override
public void onMapReady(GoogleMap map)
googleMap = map;
);
【讨论】:
【参考方案13】:替换此代码
<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_
android:layout_ />
和
if (googleMap == null)
mapFrag = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
mapFrag.getMapAsync(this);
else
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
【讨论】:
isGooglePlayServicesAvailable(this) 已弃用【参考方案14】:只需在您的 Activity 中调用 supportMapFragment.getMapAsync(this);
in onResume
【讨论】:
【参考方案15】:在 onCreate 中试试这个代码
if (mapFragment != null)
mapFragment.getMapAsync(this);
【讨论】:
【参考方案16】:如果您使用的是 Fragment,请更改此代码
SupportMapFragment mapFragment =getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
到
supportMapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
同时检查 XML 文件中的更改
android:name="com.google.android.gms.maps.MapFragment"
到
android:name="com.google.android.gms.maps.SupportMapFragment"
【讨论】:
以上是关于mapFragment.getMapAsync(this) - NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章
如何求解:T(n) = T(n/2) + T(n/4) + T(n/8) + (n)