在listView上调用addFooterView添加按钮时出现ClassCastException

时间:2016-12-27 11:35:14

标签: android listview android-fragments

我在列表视图上使用listView和页脚按钮创建片段时遇到问题。当我在listView上调用addFooterView()时,运行我的应用程序时出现以下异常:

E/AndroidRuntime: FATAL EXCEPTION: main
          Process: com.joshuaindustries.FindFun, PID: 5164
          java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
              at android.widget.ListView.clearRecycledState(ListView.java:545)
              at android.widget.ListView.resetList(ListView.java:532)
              at android.widget.ListView.layoutChildren(ListView.java:1556)
              at android.widget.AbsListView.onLayout(AbsListView.java:2162)
              at android.view.View.layout(View.java:17637)
              at android.view.ViewGroup.layout(ViewGroup.java:5575)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
              at android.view.View.layout(View.java:17637)
              at android.view.ViewGroup.layout(ViewGroup.java:5575)
              at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1767)
              at android.view.View.layout(View.java:17637)
              at android.view.ViewGroup.layout(ViewGroup.java:5575)
              at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)
              at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
              at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1364)
              at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:846)
              at android.view.View.layout(View.java:17637)
              at android.view.ViewGroup.layout(ViewGroup.java:5575)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
              at android.view.View.layout(View.java:17637)
              at android.view.ViewGroup.layout(ViewGroup.java:5575)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
              at android.view.View.layout(View.java:17637)
              at android.view.ViewGroup.layout(ViewGroup.java:5575)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
              at android.view.View.layout(View.java:17637)
              at android.view.ViewGroup.layout(ViewGroup.java:5575)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
              at android.view.View.layout(View.java:17637)
              at android.view.ViewGroup.layout(ViewGroup.java:5575)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
              at com.android.internal.policy.DecorView.onLayout(DecorView.java:726)
              at android.view.View.layout(View.java:17637)
              at android.view.ViewGroup.layout(ViewGroup.java:5575)
              at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)
              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)
              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
              at android.view.Choreographer.doCallbacks(Choreographer.java:686)
              at android.view.Choreographer.doFrame(Choreographer.java:621)
              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
              at android.os.Handler.handleCallback(Handler.java:751)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:154)
              at android.app.ActivityThread.main(ActivityThread.java:6119)
              at java.lang.reflect.Method.invoke(Native Method)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我的片段(overview_tab_fragment.xml)的布局如下所示:

        <?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="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/load_more_events"
        android:text="@string/button_load_more_events"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentBottom="true" />
</LinearLayout>

这是我的片段类(CustomListFragment.java)的源代码:

package com.joshuaindustries.CustomFragments;

import com.joshuaindustries.FindFun.DetailsActivity;
import com.joshuaindustries.data.Persons;
import android.R;
import android.R.id;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AbsListView.LayoutParams;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
 * Created by Joshua on 21/12/2016.
 */
public class CustomListFragment extends ListFragment {
    String TAG = "CustomListFragment";
    private View viewRoot;
    private Button loadMoreEventsButton;

    private SimpleAdapter listAdapter;
    private Persons personsInfo;
    private List < Persons.Person > personData;
    private ArrayList < HashMap < String, String > > adapterData;
    private String [ ] from;
    private int [ ] to;

    @Override
    public View onCreateView ( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
        super.onCreateView ( inflater, container, savedInstanceState );
        Log.d ( TAG, "onCreateView started" );
        viewRoot = inflater.inflate ( com.joshuaindustries.FindFun.R.layout.overview_tab_fragment, container, false );
        return ( viewRoot );
    }

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

        loadMoreEventsButton = ( Button ) viewRoot.findViewById ( com.joshuaindustries.FindFun.R.id.load_more_events );
        getListView ( ).addFooterView ( (View) loadMoreEventsButton );
        Log.d ( TAG, "onActivityCreated: added footer view " );
    }

    @Override
    public void onListItemClick ( ListView listView, View view, int viewPosition, long rowID ) {
        Log.d ( TAG, "viewPosition " + viewPosition );
        Log.d ( TAG, "rowID" + rowID );
        Toast onItemClickToast = Toast.makeText ( getContext ( ), ( String ) ( ( HashMap ) listAdapter.getItem ( ( int ) rowID ) ).get ( from [ 0 ] ), Toast.LENGTH_LONG );
        onItemClickToast.show ( );

        Intent detailsIntent = new Intent ( getContext ( ), DetailsActivity.class );
        Persons.putInIntent ( personsInfo.getPersonAtPosition ( ( int ) rowID ), detailsIntent );
        startActivity ( detailsIntent );
    }
}

我尝试了herehere这些建议无济于事。

先谢谢你的帮助, 约书亚

3 个答案:

答案 0 :(得分:0)

替换你的

导入
<?php
$main="prod.mail.com";

import android.widget.AbsListView.LayoutParams;

答案 1 :(得分:0)

实际上您收到此异常是因为您尝试添加与 Linearlayout 父级关联的按钮,因为您使用FindViewById()添加视图,因此添加到Listview的视图将有 LinearLayout参数

您可以将按钮移动到另一个xml中,如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/load_more_events"
        android:text="Load more"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"/>

</RelativeLayout>

在代码中您可以使用它,如下所示

RelativeLayout footerview = (RelativeLayout) getLayoutInflater().inflate(R.layout.footer_view, getListView(),false);
getListView().addFooterView(footerview);

所以在这里当我给xml充气时,我将 Listview 作为父级传递,所以现在Footerview将有 AbsListView $ LayoutParams ,因此您的问题将得到解决

答案 2 :(得分:0)

Hasifs的回答解决了我的问题。谢谢!你让我走上正确的道路来解决我的问题,我很感激解释。这次我确实选择在代码中创建我的按钮(为了保持我的文件夹有些可管理):

Button loadMoreEventsButton = new Button ( getContext ( ) );
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams ( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT );
layoutParams.addRule ( RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE );
loadMoreEventsButton.setLayoutParams ( new AbsListView.LayoutParams ( layoutParams ) );
loadMoreEventsButton.setText ( "Load more events ..." );
getListView ( ).addFooterView ( loadMoreEventsButton );

祝你有愉快的一天! ;)