0

我有一个基于 tabHost 的应用程序,带有片段,如果我将片段与 LinearLayout 一起使用,它可以正常工作

但我需要在其中一个选项卡视图上列出一个列表 [片段]

但应用程序崩溃,因为没有正确充气:我的班级[标签视图片段]:

public class Tab2Fragment extends Fragment { //primera pregunta, lo puedo hacer asi o con FragmentActivity
//public class Tab2Fragment extends ListFragment { //primera pregunta, lo puedo hacer asi o con FragmentActivity


    private FragmentManager fm;
    private ListFragment list;
    private List<Map<String, String>> listItems;
    private String[] froms;
    private int[] viewIds;

    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
     */


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }
        return (LinearLayout)inflater.inflate(R.layout.tab_frag2_layout, container, false);
    }



}

我的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="#00FF00">

        <fragment
        android:name="android.support.v4.app.ListFragment"
        android:id="@+id/List"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

错误:

05-31 22:39:58.412: E/AndroidRuntime(2796): FATAL EXCEPTION: main
05-31 22:39:58.412: E/AndroidRuntime(2796): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
05-31 22:39:58.412: E/AndroidRuntime(2796):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
05-31 22:39:58.412: E/AndroidRuntime(2796):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
05-31 22:39:58.412: E/AndroidRuntime(2796):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
05-31 22:39:58.412: E/AndroidRuntime(2796):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-31 22:39:58.412: E/AndroidRuntime(2796):     at com.orchard.elasto.Tab2Fragment.onCreateView(Tab2Fragment.java:54)
05-31 22:39:58.412: E/AndroidRuntime(2796):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)

那么,如何正确地扩充列表呢?

我应该用fragmentList来做吗,即使tabHost正在指定一个片段?

public void onTabChanged(String tag) {
        TabInfo newTab = this.mapTabInfo.get(tag);
        if (mLastTab != newTab) {
            FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
            if (mLastTab != null) {
                if (mLastTab.fragment != null) {
                    ft.detach(mLastTab.fragment);
                }
            }
            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment = Fragment.instantiate(this,
                            newTab.clss.getName(), newTab.args);
                    ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
                } else {
                    ft.attach(newTab.fragment);
                }
            }

            mLastTab = newTab;
            ft.commit();
            this.getSupportFragmentManager().executePendingTransactions();
        }
    }

多谢!!

4

1 回答 1

1

你的问题在这里:

   android:name="android.support.v4.app.ListFragment" 

您告诉它运行该片段的类是 ListFragment。

您需要更改它以引用您的类,并且需要将该类设为 ListFragment 而不是 Fragment。

   android:name="your.package.name.Tab2Fragment" 
于 2012-05-31T13:17:03.247 回答