最简单的代码在Activity调用Fragment时工作,但在调用ListFragment时不工作

时间:2013-10-14 11:19:38

标签: android android-fragments

在使用extends Fragment但不在ListFragment时使用。

主要活动:

public class FagTabHostMain extends FragmentActivity {
    FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_fag_tab_host_main);

            mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
            mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

            mTabHost.addTab(mTabHost.newTabSpec("more").setIndicator("More"),
                    More.class, null);

        } catch (Exception e) {
            Log.e("TAG", e.toString());
        }

    }

}

使用以下内容:

public class More extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.more, null);
        return view;
    }
}

但不是:

public class More extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.more, null);
        return view;
    }
}

1 个答案:

答案 0 :(得分:1)

R.layout.moreListView必须是带有android:id="@android:id/list"标记的<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> ,例如:

{{1}}