一个片段内的两个ListFragment

时间:2012-04-09 09:29:13

标签: android android-fragments android-listfragment

我在android中使用android兼容性支持包创建了以下内容

enter image description here

基本上我有一个FragmentAcitivity,显示两个Tabs。一个标签代表Fragment #1,第二个标签代表Fragment #2。片段#2有两个ListFragment,即ListFragment#1和ListFragment#2,但问题是我在尝试这样做时遇到以下错误。

    android.view.InflateException: Binary XML file line #10: Error inflating class fragment
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
     at com.myexample.template.TextVoiceFragment.onCreateView(TextVoiceFragment.java:19)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:837)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1041)
     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1359)
     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:422)
     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:86)
     at android.support.v4.view.ViewPager.populate(ViewPager.java:453)
     at android.support.v4.view.ViewPager.onAttachedToWindow(ViewPager.java:563)
     at android.view.View.dispatchAttachedToWindow(View.java:6015)
     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1124)
     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1129)
     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1129)
     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1129)
     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1129)
     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:1129)
     at android.view.ViewRoot.performTraversals(ViewRoot.java:731)
     at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:143)
     at android.app.ActivityThread.main(ActivityThread.java:4914)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalStateException: Content view not yet created
     at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
     at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
     at com.myexample.template.VoiceTemplate.onActivityCreated(VoiceTemplate.java:82)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:858)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1020)
     at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1099)
     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:263)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)

二进制XML

<fragment class="com.myexample.template.VoiceTemplate" 
    android:id="@+id/voiceListFragment"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1" 
/>

第19行包含 在TextVoiceFragment.class的onCreateView中,在这种情况下是片段#2

 return inflater.inflate(R.layout.text_voice_fragment, container, false);

行号82包含 在VoiceTemplate.class的onActivityCreated内的代码中,它扩展了ListFragment&amp;一个ListFragment#2

// 
getListView().setOnItemClickListener( new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view,
            int position, long arg3) {
        // TODO Auto-generated method stub
    }

 });

我尝试用FragmentActivity替换Fragment#2,如下所示

enter image description here

但在这种情况下我得到了以下错误

 java.lang.ClassCastException: com.myexample.template.TextVoiceFragment 

这里TextVoiceFragment是Tab第二个

2 个答案:

答案 0 :(得分:1)

我不确定这是否会造成您的问题,但将来您可能会有其他问题,因为不支持嵌套片段。

您可以阅读Android框架工程师的答案:Android: Can you nest Fragments?

答案 1 :(得分:0)

您可以拥有一个包含两个ListFragments的Activity。这基本上就是你在GMail应用程序中看到的。如果你想让Fragment#1出现在ListFragment#1和ListFragment#2旁边,那么你必须在同一个Layout或FragmentManager中将所有3个作为兄弟姐妹。