如何用Fragment中的视图替换Fragment

时间:2015-05-13 05:49:04

标签: android android-fragments

我正在编写一个在其中显示PreferenceFragment的DialogFragment,但它不起作用

SettingDialogFragment.java

package uet.dtui.megaereader;

import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

public class SettingDialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_book_setting, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        View root = getView();
        FrameLayout frame = (FrameLayout) root.findViewById(R.id.content_fm_frame);
        System.out.println(frame);

        /* My stub ...*/
        SettingsFragment setting = new SettingsFragment();
        Bundle bundle = new Bundle();
        bundle.putBoolean(SettingsFragment.BOOK_SETTING_ONLY, true);
        setting.setArguments(bundle);
        /*             */

        getFragmentManager().beginTransaction()
            .replace(R.id.content_fm_frame, setting)
            .commit();
    }

}

layout_book_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/content_fm_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

我收到错误:

  

找不到ID为0x7f0e0093的视图   片段的(uet.dtui.megaereader:id / content_fm_frame)   SettingsFragment {53664f7c#2 id = 0x7f0e0093}

但是替换之前的System.out语句仍然有效:

  

android.widget.FrameLayout {53539680 V.E ..... ......我。 0,0-0,0

     

7f0e0093 app:id / content_fm_frame}

那么,如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

避免嵌套片段。因为您可以在片段检测中获得非法状态异常。你可以在三星上得到这个例外 使用kitkat的设备。这是android中关于嵌套片段的已知错误

  

getActivity()。getChildFragmentManager()。代替(R.id.content_fm_frame,   fragment_instance).commit();

相关问题