android - 膨胀类 fragment : GoogleMaps inside FrameLayout 时出错

标签 android

我正在尝试在 FrameLayout 中显示 GoogleMaps。此 FrameLayout 应该根据切换按钮上下移动。但是,当我尝试膨胀 View 时,我的应用程序崩溃了。

我在这里做的事情有可能吗?我的意思是在 FrameLayout 中显示 GoogleMaps?我怎样才能使它工作?

谢谢!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/fragment_a"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/lv_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom"
            android:text="Hello World!"/>
    </RelativeLayout>
    <!-- this frame is invisible -->
    <FrameLayout
        android:id="@+id/fragment_b"
        android:visibility="invisible"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:background="#FF0000">
        <fragment
            android:id="@+id/map_v2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.google.android.gms.maps.SupportMapFragment" />
    </FrameLayout>
</LinearLayout>

我的 Fragment 类中的相应代码:

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

    final View rootView            = inflater.inflate(R.layout.fragment_favorites, container, false);
    final FragmentManager fm       = getFragmentManager();
    final RelativeLayout fragmentA = (RelativeLayout) rootView.findViewById(R.id.fragment_a);

    fragmentA.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            RelativeLayout fragmentA = (RelativeLayout) getActivity().findViewById(R.id.fragment_a);
            FrameLayout fragmentB    = (FrameLayout)getActivity().findViewById(R.id.fragment_b);

            int y = 200;
            if (state == true) {
                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    fragmentA.animate().translationY(-y).setDuration(500);
                    fragmentB.animate().translationY(-y).setDuration(500);
                    state = false;
                }
            } else {
                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    fragmentA.animate().translationY(y).setDuration(500);
                    fragmentB.animate().translationY(y).setDuration(500);
                    state = true;
                }
            }
        }
    });
    return rootView;
}

我得到的错误:

04-19 15:39:43.248    8758-8758/com.mahlzeit E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mahlzeit, PID: 8758
    android.view.InflateException: Binary XML file line #20: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at com.mahlzeit.mahlzeitactivity.FavoritesFragment.onCreateView(FavoritesFragment.java:27)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)

最佳答案

看起来你在嵌套 fragment 。 Fragment 布局中不能有 标签。

检查这里: Fragments within Fragments

使用 getChildFragmentManager() 动态添加 fragment 在你的 fragment 中。

关于android - 膨胀类 fragment : GoogleMaps inside FrameLayout 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29730820/

相关文章:

android - 如何在 Android 中渲染数学方程式

android - FFmpegMediaMetadataRetriever.getFrameAtTime() 进展缓慢

Android,如何创建选项菜单

java - 改变 Activity 方向的填充

java - 连接移动设备 (Android) 作为 MTP,现在我们可以访问此设备并获取时间

android - 来自 URL 的 setimageBitmap 不适合我想要的大小

android - 如何将自定义库添加到 Android Studio 2.2.3

android - picasso 不会加载谷歌静态 map

java - XML 转换为 JSON 时前导零被截断

android - Android 上的 Card.IO 与 Gradle