如何使用CursorLoader在ListFragment中实现Header

时间:2013-02-04 01:11:41

标签: android android-listfragment android-cursorloader

尝试将标题放在像这样的ListFragment中

@Override
public View onCreateView(org.holoeverywhere.LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View listRoot = inflater.inflate(R.layout.list, null);
    headerView = inflater.inflate(R.layout.header);
    return listRoot;
}

在onActivityCreated

@Override
public void onActivityCreated(Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);


    setEmptyText("No Contacts");
    setHasOptionsMenu(true);

    ListView listView = getListView();
    //String headerData = "ZZZZZZZZ";
    if(headerView != null) listView.addHeaderView(headerView);

    String[] from = {TableMetaData.NAME};
    int[] to = {R.id.menu_name};

    customListAdapter = new ContactListAdapter(getActivity(), R.layout.menu_row,
            null, from, to, 0);
    setListAdapter(customListAdapter);
    setListShown(false);
    getLoaderManager().initLoader(0, null, this).forceLoad();

}

我在Logcat中收到以下错误

02-04 05:53:09.287: E/AndroidRuntime(22767): FATAL EXCEPTION: main
02-04 05:53:09.287: E/AndroidRuntime(22767): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.xyz/com.abc.xyz.mnp.Mnp}: java.lang.IllegalStateException: Can't be used with a custom content view
02-04 05:53:09.287: E/AndroidRuntime(22767):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
02-04 05:53:09.287: E/AndroidRuntime(22767):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
02-04 05:53:09.287: E/AndroidRuntime(22767):    at android.app.ActivityThread.access$2300(ActivityThread.java:126)
02-04 05:53:09.287: E/AndroidRuntime(22767):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
02-04 05:53:09.287: E/AndroidRuntime(22767):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 05:53:09.287: E/AndroidRuntime(22767):    at android.os.Looper.loop(Looper.java:123)
02-04 05:53:09.287: E/AndroidRuntime(22767):    at android.app.ActivityThread.main(ActivityThread.java:4633)
02-04 05:53:09.287: E/AndroidRuntime(22767):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 05:53:09.287: E/AndroidRuntime(22767):    at java.lang.reflect.Method.invoke(Method.java:521)
02-04 05:53:09.287: E/AndroidRuntime(22767):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-04 05:53:09.287: E/AndroidRuntime(22767):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-04 05:53:09.287: E/AndroidRuntime(22767):    at dalvik.system.NativeStart.main(Native Method)

对我而言,它似乎要求我从setEmptyText("No Contacts");

中删除setListShown(false);setListShown(true);以及public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {

我的问题

为什么会出现这个错误?如何避免/规避这个错误?

0 个答案:

没有答案