android - listview.addHeaderView()添加标题头出错
PHPz
PHPz 2017-04-17 17:53:08
0
1
548

我在写一个布局时,在listview中添加一个标题头时出错
以下是我的代码

public Newspager(Context context,String url) {
        mInflate = View.inflate(context, R.layout.news_pager, null);
        ListView newspagerlv = (ListView) mInflate.findViewById(R.id.news_pager_lv);


        View header = View.inflate(context, R.layout.header, null);

        mAuto_roll = (AutoRolllayout) header.findViewById(R.id.auto_roll_out);
        //添加头到listview
        newspagerlv.addHeaderView(mAuto_roll);

        newspagerlv.setAdapter(mBaseAdapter);
        }

layout.news_pager布局很简单,就是一个线性布局包裹一个listview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <ListView
        android:id="@+id/news_pager_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

R.layout.header作为头添加到listview中,以下是代码,里面的自定义控件是没有什么问题的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    <helloas.siinax.com.lib_autorollayout.widget.AutoRolllayout
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:id="@+id/auto_roll_out"
        />
</LinearLayout>

现在就是将这个自定义控件作为头添加到listview中,就出错了,也就是
newspagerlv.addHeaderView(mAuto_roll);
往listview中添加头时出错
哪位神帮忙解决一下!

打印的logcat

 FATAL EXCEPTION: main
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.widget.ListView.clearRecycledState(ListView.java:515)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.widget.ListView.resetList(ListView.java:501)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.widget.ListView.setAdapter(ListView.java:444)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at helloas.siinax.com.itcastnews.fragment.Newspager.<init>(Newspager.java:61)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at helloas.siinax.com.itcastnews.fragment.Newsfragment$2.instantiateItem(Newsfragment.java:156)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.support.v4.view.ViewPager.populate(ViewPager.java:1020)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:946)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2910)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at helloas.siinax.com.itcastnews.fragment.Newsfragment$1.onSuccess(Newsfragment.java:104)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at com.lidroid.xutils.http.HttpHandler.onProgressUpdate(HttpHandler.java:218)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at com.lidroid.xutils.task.PriorityAsyncTask$InternalHandler.handleMessage(PriorityAsyncTask.java:385)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5041)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-12 14:00:45.116 2958-2958/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

PHPz
PHPz

学习是最好的投资!

모든 응답(1)
巴扎黑

헤드뷰는 헤더 아닌가요? addHeaderView가 대신 mAuto_roll을 사용하는 이유는 무엇입니까?
이렇게 해야지

으아아아
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿