[英]How to replace Fragment with a view inside a Fragment
我正在寫一個DialogFragment,其中顯示一個PreferenceFragment,但它不起作用
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)的視圖Fragment {53664f7c#2 id = 0x7f0e0093}
但是替換之前的System.out
語句仍然有效:
android.widget.FrameLayout {53539680 VE .... 0,0-0,0
7f0e0093 app:id / content_fm_frame}
那么,如何解決這個問題呢?
避免嵌套碎片。 因為您可以在片段檢測中獲得非法狀態異常。 你可以在三星上得到這個例外攻勢
使用kitkat的設備。 這是android中有關嵌套片段的已知錯誤
getActivity()。getChildFragmentManager()。replace(R.id.content_fm_frame,fragment_instance).commit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.