我有三個片段和它們佈置,如下所示:的Android - 嵌套片段沒有查看找到
「分片A和分片C不相連,但分片B和C是在每個片段包含1名列表視圖如此。每次我觸發一個事件時,它都會轉到下一個片段。「
片段A --->(內部破片
A
)片段B --->(內部破片B
)片段C
- 片段A(main.xml中)
- 片段B (R.id.contentFragment)
- 片段C(R.id.contentFragment)
我可以顯示Fragmen t A和片段B並且沒問題,但是如果我從B
調用片段C,那麼我得到了"No view found in contentFragment=0x7f040039"
。
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/white"
android:id="@+id/frameLayout"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
>
</ListView>
<FrameLayout
android:id="@+id/contentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>
</FrameLayout>
</FrameLayout>
FragmentA.java
內部片段A打開片段B
Fragment fragment1 = new FragmentB();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.contentFragment, fragment1, fragMainGroups);
transaction.addToBackStack(fragMainGroups);
transaction.commit();
FragmentB.java
內部片段B打開片段C
String fragGroups = "groups";
Fragment fragment1 = new FragmentC();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.contentFragment, fragment1, fragGroups);
transaction.addToBackStack(fragGroups);
transaction.commit();
FragmentC.java
內部片段C
public class FragmentItems extends ListFragment{
public void onAttach(Activity activity) {
super.onAttach(activity);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void onCreate(Bundle e)
{
super.onCreate(e);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.load_items_activity, container, false);
return rootView;
}
}
logcat的
07-25 18:10:20.388: E/FragmentManager(24703): No view found for id 0x7f040039 (com.jinisys.restoplusordering:id/contentFragment) for fragment FragmentItems{4156f480 #0 id=0x7f040039 groups}
07-25 18:10:20.388: E/FragmentManager(24703): Activity state:
07-25 18:10:20.388: E/FragmentManager(24703): No view found for id 0x7f040039 (com.jinisys.restoplusordering:id/contentFragment) for fragment FragmentItems{4156f480 #0 id=0x7f040039 groups}
07-25 18:10:20.388: E/FragmentManager(24703): Activity state:
07-25 18:10:20.718: E/AndroidRuntime(24703): FATAL EXCEPTION: main
07-25 18:10:20.718: E/AndroidRuntime(24703): java.lang.IllegalArgumentException: No view found for id 0x7f040039 (com.jinisys.restoplusordering:id/contentFragment) for fragment FragmentItems{4156f480 #0 id=0x7f040039 groups}
07-25 18:10:20.718: E/AndroidRuntime(24703): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
07-25 18:10:20.718: E/AndroidRuntime(24703): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-25 18:10:20.718: E/AndroidRuntime(24703): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-25 18:10:20.718: E/AndroidRuntime(24703): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-25 18:10:20.718: E/AndroidRuntime(24703): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
07-25 18:10:20.718: E/AndroidRuntime(24703): at android.os.Handler.handleCallback(Handler.java:615)
請假設'contentFragmentTtems'已被刪除,我不需要這個先生。請參閱更新的代碼。我想替換Fragment C中包含的'contentFragment',就像我在Fragment A中所做的那樣。我只使用'main.xml'來顯示嵌套的片段。 – rahstame
那麼如果你想用片段C替換你的片段B,那麼是的,你必須替換'contentFragment',但正如我之前所說的,你必須從片段A而不是從片段B做到這一點。 – Desert
我相信如果我明白你對這位先生的意思,「從片段A而不是片段B做這個」。 'Frag A'和'Frag C'沒有連接先生,但'Frag B'和'C'是。每個片段包含1個列表視圖,因此每次觸發一個事件時都會轉到下一個片段。 – rahstame