0

我正在尝试向我的 ListView 添加标题,但不幸的是我收到了这个错误:

10-28 01:35:29.394: E/AndroidRuntime(17389): FATAL EXCEPTION: main
10-28 01:35:29.394: E/AndroidRuntime(17389): java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to com.hb.views.PinnedSectionListView$PinnedSectionListAdapter
10-28 01:35:29.394: E/AndroidRuntime(17389):    at com.hb.views.PinnedSectionListView$2.onScroll(PinnedSectionListView.java:91)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1340)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.widget.ListView.layoutChildren(ListView.java:1762)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.widget.AbsListView.onLayout(AbsListView.java:2012)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.View.layout(View.java:14296)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.View.layout(View.java:14296)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.View.layout(View.java:14296)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.View.layout(View.java:14296)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:678)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.View.layout(View.java:14296)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.View.layout(View.java:14296)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:349)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.View.layout(View.java:14296)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.View.layout(View.java:14296)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.ViewGroup.layout(ViewGroup.java:4562)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.Choreographer.doFrame(Choreographer.java:532)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.os.Handler.handleCallback(Handler.java:730)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.os.Looper.loop(Looper.java:137)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at android.app.ActivityThread.main(ActivityThread.java:5289)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at java.lang.reflect.Method.invokeNative(Native Method)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at java.lang.reflect.Method.invoke(Method.java:525)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
10-28 01:35:29.394: E/AndroidRuntime(17389):    at dalvik.system.NativeStart.main(Native Method)

我的代码:

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

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    TextView header = new TextView(getActivity());
    header.setText("Header");
    getListView().addHeaderView(header);

    // ... adding objects to the adapter ...

    ArticleAdapter adapter = new ArticleAdapter(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, objects);
    setListAdapter(adapter);
}

我正在使用 pinned-section-listview 库。

先感谢您!

4

1 回答 1

1

您尝试添加标题 getListView().addHeaderView(header);

这会导致 listview 将适配器包装在另一个适配器中HeaderViewListAdapter,而 library 希望它是自己的适配器

// get expected adapter or fail
PinnedSectionListAdapter adapter = (PinnedSectionListAdapter) view.getAdapter();

您要么修复使用过的库,要么将错误发布给它的作者,或者尽量不使用addHeader

于 2013-10-28T00:38:18.433 回答