android - 在框架布局中访问动态添加的谷歌地图 fragment

标签 android google-maps android-fragments

我已经在框架布局中动态添加了我的谷歌地图 fragment 。现在我想用谷歌地图做标记和做其他事情。但我无法通过获取 fragment 管理器访问它。代码没有显示错误,但执行时返回空指针异常。

我认为是逻辑错误。与类型转换有关。请帮忙

这是我的“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> 

这是我的类(class)

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();

它正在获取其父 View 而不是 subview ,所以用这一行替换它

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

一切都很好:).

关于android - 在框架布局中访问动态添加的谷歌地图 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22570539/

相关文章:

java - 为什么我收到 'UNIQUE constraint failed' 错误

android - Android AppWidgets 是否限于特定尺寸?

android - 动画相机以在谷歌地图中定位和设置平移

Java算法将土地坐标转换为Google map 格式

android - 在没有 UI 的情况下添加其他 fragment 后获取 "Fragment did not create a view"

android - FragmentStatePageAdapter 不会暂停 fragment

android - MediaPlayer 开始在状态 0(错误 -38、0)中调用,android?

java - Android 调用 REST 服务。未找到来源

android - 显示离我当前位置最近的标记的标题 Google Maps v2

android - fragment 在背景中可见