java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams

时间:2014-07-18 18:13:10

标签: java android mobile classcastexception listadapter

我正在使用BaseExpandableListAdapter,如下所示。而不是传递列表,我直接传递一个视图作为子数据。并且子计数总是返回1.因为每个标题只有一个视图。

public class TaxAdapter extends BaseExpandableListAdapter{

    private Context _context;
    private ArrayList<String> _listDataHeader; 
    private HashMap<String, View> _listDataChild;


    public TaxAdapter(Context context, ArrayList<String> taxListDataHeader,
            HashMap<String, View> taxListDataChild) {
        this._context = context;
        this._listDataHeader = taxListDataHeader;
        this._listDataChild = taxListDataChild;
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
         return this._listDataHeader.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
         return this._listDataChild.get(this._listDataHeader.get(groupPosition));
    }

    @Override
    public long getGroupId(int groupPosition) {
         return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LinearLayout earningsContainer = new LinearLayout(_context);
            LayoutInflater inflator = ((Activity)_context).getLayoutInflater();
            convertView = inflator.inflate(R.layout.row_tax_explv_title,
                    earningsContainer, false);
            ((TextView) convertView.findViewById(R.id.explvTaxTitleText)).setText(headerTitle);
        }

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        return (View)getChild(groupPosition, childPosition);

    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

}

并且从活动传递的数据如下 -

    ArrayList<String> TaxListDataHeader = new ArrayList<String>();
    HashMap<String, View> TaxListDataChild = new HashMap<String, View>();

TaxListDataHeader.add("Movie Shows");
TaxListDataChild.put("Movie Shows", moviesContainerView);
TaxListDataHeader.add("Theatre Shows");
TaxListDataHeader.add("Theatre Shows", theatreContainerView);

movieContainerView和theareContainerView是两个自定义的膨胀线性布局。像这样

LayoutInflater inflator = ((Activity) context).getLayoutInflater();
        View row = inflator.inflate(R.layout.row_movie_data,
                movieMainContainer, false);

        ((TextView) row.findViewById(R.id.txtEmpInfoPopUpTitle)).setText(key);
        ((TextView) row.findViewById(R.id.txtEmpInfoPopUpValue)).setText(value);
        return row;

我收到标题中的错误,但日志没有指定任何文件名。 请提出错误发生的位置。请建议可以做些什么。

修改 这是堆栈跟踪

07-17 22:53:57.043: E/AndroidRuntime(5074): FATAL EXCEPTION: main
07-17 22:53:57.043: E/AndroidRuntime(5074): Process: com.synthesize.paysal, PID: 5074
07-17 22:53:57.043: E/AndroidRuntime(5074): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.ListView.measureScrapChild(ListView.java:1183)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.ListView.onMeasure(ListView.java:1149)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1226)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.ScrollView.onMeasure(ScrollView.java:326)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.Choreographer.doFrame(Choreographer.java:544)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.os.Handler.handleCallback(Handler.java:733)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.os.Looper.loop(Looper.java:136)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at java.lang.reflect.Method.invokeNative(Native Method)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at java.lang.reflect.Method.invoke(Method.java:515)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-17 22:53:57.043: E/AndroidRuntime(5074):     at dalvik.system.NativeStart.main(Native Method)

修改 我试图用这样的列表视图包装线性布局,

  

TaxListDataChild.put(&#34; Theatre Show&#34;,(new   ListView控件(上下文))。addView(theatrerowContainer,   LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

但似乎第二个参数是无效的。

我正在填充&#34;新的Linearlayout&#34; xml中的(earningsContainer)与其他线性布局行一样。

for (int i = 0; i < taxMainObject.getTaxEarnings().size(); i++) {
        earningsContainer.addView(getTaxEarningsRow(taxMainObject
                    .getTaxEarnings().get(i)));
            totalEarnings += taxMainObject.getTaxEarnings().get(i)
                    .getTotalAmount();
        }
        earningsContainer.addView(getTaxTotalRow("GROSS TOTAL SALARY",
                totalEarnings + ""));

并且行的xml是 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:orientation="horizontal"
    android:background="#ffeeeeee" >


   <TextView
        android:id="@+id/txtTotalText"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:textAppearance="@android:attr/textAppearanceSmall"
        android:textColor="#736F6E"
        android:textSize="14sp"
        android:textStyle="bold|italic" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right|center_vertical"
        android:orientation="horizontal"
        android:padding="2dp" >

        <TextView
            android:id="@+id/txtTotalValue"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAppearance="@android:attr/textAppearanceSmall"
            android:textColor="#736F6E"
             android:gravity="right"
            android:textSize="14sp"
            android:layout_weight="1"
            android:textStyle="italic" />
        </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

虽然这与你的java无关但我仍然有同样的错误。我已经&#34;扩展了AppCompatActivity&#34;在我的班级,但当我用&#34;延长活动&#34;错误已解决。希望这对其他人有帮助,因为我被困了三个小时。很乐意提供帮助

答案 1 :(得分:0)

android.widget.ListView $ LayoutParams是android.widget.ViewGroup $ LayoutParams的子类,而android.widget.AbsListView也是同一个子类,即兄弟关系,因此类型转换将抛出异常。 参考

http://developer.android.com/reference/android/widget/AbsListView.LayoutParams.html

这表明您无法直接将视图作为子视图传递给listview适配器,而是尝试传递Hashmap并动态地扩展视图。 试试这个:

   @Override
    public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
 }

其中listDataHeader是传递给适配器的哈希映射,如

public ExpandableListAdapter(Context context, List<String> listDataHeader,
        HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

(通过HashMap而不是View)