Kav*_*bhu 9 android nullpointerexception mapactivity android-fragments supportmapfragment
我已经实现了一个MapFragment在运行时添加的活动.该MapFragmentXML有静态的fragment,我试图让添加在运行时.我还发现Lollipop在运行时添加了地图片段存在一些问题.请检查问题和临时解决方案
我也在下面给出了我的代码,
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>
Run Code Online (Sandbox Code Playgroud)
MapsFragment.java
器物 onMapReadyCallback
public class MapsFragment extends SupportMapFragment implements OnMapReadyCallback
Run Code Online (Sandbox Code Playgroud)
在onResume回调中
@Override
public void onResume() {
super.onResume();
((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}
Run Code Online (Sandbox Code Playgroud)
这总是给我null,我也尝试过,
((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
Run Code Online (Sandbox Code Playgroud)
这也回归了 NullPointerException
MapsActivity.java
getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, MapsFragment.newInstance()).commit();
Run Code Online (Sandbox Code Playgroud)
我在onCreateActivity回调方法中添加了这个.
我无法弄清楚为什么我还在这里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
Run Code Online (Sandbox Code Playgroud)
Arn*_*liu 15
我终于通过观察SO中给出的答案为自己解决了这个问题.我比较了代码和答案中的代码.在我看来,这个API存在一些混乱,Google的实施非常广泛.我反复抛出这个异常:
Attempt to invoke interface method 'void com.google.maps.api.android.lib6.e.fl.o()' on a null object reference
Run Code Online (Sandbox Code Playgroud)
我仍然不知道在SO问题中的某些实现是否有相同的异常,但这对我有用,并且可能对其他人有效.
这些是你需要检查的东西:
确保你有这两个(我只有api关键元数据)
<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" />
Run Code Online (Sandbox Code Playgroud)你有一个像这样的xml片段,class属性是正确的,如下所示
<fragment
android:id="@+id/google_map_fragment"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)在我的例子中,地图片段是一个孩子,在另一个片段中.
这个父片段显然不应该是MapFragment或SupportMapFragment.将它从SupportMapFragment更改为Fragment后,地图显示完美.
public class ParentFragment extends Fragment {
Run Code Online (Sandbox Code Playgroud)
我把其他所有内容都保留在原来的状态,但不是在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
}
}
}
Run Code Online (Sandbox Code Playgroud)
我没有改变任何其他东西,我没有弄乱rootView或parentView.正如你在@Konstantin Loginov的回答中看到的那样
尝试一下,让我知道.
| 归档时间: |
|
| 查看次数: |
9359 次 |
| 最近记录: |