【发布时间】:2016-04-07 06:45:40
【问题描述】:
我已经实现了一个在运行时添加MapFragment 的活动。 MapFragment xml 具有静态 fragment,我试图在运行时添加。我还发现 Lollipop 在运行时添加地图片段存在一些问题。请检查Issue raised和temporary solution
我也在下面给出了我的代码,
fragment_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=".fragment.MapsFragment">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="appCreators.bloodfinder.activity.MapsActivity"/>
<include
android:id="@+id/layout"
layout="@layout/template_custom_spinner"/>
</FrameLayout>
MapsFragment.java
实现onMapReadyCallback
public class MapsFragment extends SupportMapFragment implements OnMapReadyCallback
在onResume回调中
@Override
public void onResume() {
super.onResume();
((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}
这总是返回 null 我也尝试过,
((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
这也返回NullPointerException
MapsActivity.java
getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, MapsFragment.newInstance()).commit();
我在onCreate Activity 回调方法中添加了这个。
我无法弄清楚为什么我仍然收到NullPointerException!
有时我会收到Attempt to invoke interface method 'void com.google.maps.api.android.lib6.e.fl.o()' on a null object reference
我们将不胜感激!
更新:仍未修复我收到以下错误。我查看了日志,但不知道为什么会这样。
Unable to resume activity {MapsActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.e.fl.o()' on a null object reference
【问题讨论】:
-
将
((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);行放入您的onCreateView() -
并从
fragment_map.xml中删除tools:context="appCreators.bloodfinder.activity.MapsActivity" -
我试过了,但没有运气我得到
Unable to resume activity {MapsActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.e.fl.o()' on a null object reference -
有什么解决办法吗?我已经坚持了 2 天了。
-
我刚刚解决了。看看我的回答,让我知道。
标签: android android-fragments nullpointerexception supportmapfragment mapactivity