android – 在framelayout中访问动态添加的谷歌地图片段

我在framelayout中动态添加了我的谷歌地图片段.现在我想把标记和谷歌地图做其他的东西.但我无法通过get片段管理器访问它.代码显示没有错误但是当它执行时它返回空指针异常.

我认为它的逻辑错误.与类型铸造有关.请帮助

这是我的“nearby_places.xml”布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map_area"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <com.localiteproximus.CustomAutoCompleteTextView
    android:id="@+id/atv_places"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"        
    android:layout_alignParentTop="true"
    android:hint="@string/str_atv_places"
    android:singleLine="true" />

    <FrameLayout
    android:id="@+id/mapView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_below="@id/atv_places">

</FrameLayout>
</RelativeLayout>

这是我的班级

public class NearByPlaceFragment extends Fragment {

public NearByPlaceFragment(){}

View rootView;
GoogleMap googleMap;

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

        MapFragment fragment = new MapFragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.mapView,fragment).commit();
 rootView = inflater.inflate(R.layout.nearby_places,container,false);

  googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
 return rootView

}

和LogCat显示这一点

58.608: E/AndroidRuntime(12654): FATAL EXCEPTION: main
03-22 03:00:58.608: E/AndroidRuntime(12654): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.localiteproximus/com.localiteproximus.NearbyPlaces}: android.view.InflateException: Binary XML file line #21: Class is not a View com.google.android.gms.maps.MapFragment
03-22 03:00:58.608: E/AndroidRuntime(12654):    at android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2294)
03-22 03:00:58.608: E/AndroidRuntime(12654): Caused by: java.lang.classCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.view.View
03-22 03:00:58.608: E/AndroidRuntime(12654):    at java.lang.class.asSubclass(Class.java:1182)
03-22 03:00:58.608: E/AndroidRuntime(12654):    at android.view.LayoutInflater.createView(LayoutInflater.java:565)
03-22 03:00:58.608: E/AndroidRuntime(12654):    ... 23 more

解决方法

我自己得到了答案.

所以这真是一个愚蠢的错误.6小时后我发现错误在这条线上

googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.mapView)).getMap();

它正在获取其父视图而不是子视图,因此将其替换为此行

googleMap = ((MapFragment) getChildFragmentManager().findFragmentById(R.id.mapView)).getMap();

一切都很好:).

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...