Android ViewHolder和classcast异常

时间:2013-06-18 21:18:41

标签: android listview

我有一个包含3种不同类型行的listView。我在线上获得ClassCastException

holder = (RowViewHolder) row.getTag();

我注意到row.getTag()返回HeaderViewHolder。为什么这样,我该如何解决?

这是我的代码:

header_row.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" >

<TextView
    android:id="@+id/categoryNameTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

queue_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="10dp"
    tools:ignore="HardcodedText,ContentDescription" >

    <ImageView
        android:id="@+id/qItemImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/qItemNameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@id/qItemImageView"
        android:gravity="center"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ProgressBar
        android:id="@+id/qProgressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/qItemNameTextView"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/qItemNameTextView"
        android:max="100"
        android:progress="50" />

    <TextView
        android:id="@+id/qLevelTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/qItemImageView"
        android:layout_alignTop="@id/qItemImageView"
        android:text="7"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/qTimeTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/qProgressBar"
        android:layout_alignLeft="@id/qProgressBar"
        android:layout_alignRight="@id/qProgressBar"
        android:layout_alignTop="@id/qProgressBar"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="01:01:01"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/black" />

</RelativeLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:ignore="HardcodedText,ContentDescription" >

    <ImageView
        android:id="@+id/itemImageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/nameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/itemImageView"
        android:gravity="center"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/levelTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/itemImageView"
        android:layout_alignTop="@id/itemImageView"
        android:text="7"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <LinearLayout
        android:id="@+id/priceLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/nameTextView"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/nameTextView"
        android:baselineAligned="false"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/woodTextView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:drawableLeft="@drawable/wood"
            android:gravity="center"
            android:text="111"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/ironTextView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:drawableLeft="@drawable/iron"
            android:gravity="center"
            android:text="111"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/stoneTextView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:drawableLeft="@drawable/stone"
            android:gravity="center"
            android:text="111"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/goldTextView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:drawableLeft="@drawable/gold"
            android:gravity="center"
            android:text="111"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </LinearLayout>

    <Button
        android:id="@+id/actionButton"
        android:layout_width="90dp"
        android:layout_height="37dp"
        android:layout_alignParentRight="true"
        android:layout_below="@id/priceLayout"
        android:text="Buduj" />

    <TextView
        android:id="@+id/timeTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/actionButton"
        android:layout_toLeftOf="@id/actionButton"
        android:layout_toRightOf="@id/itemImageView"
        android:gravity="center"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

和java文件的重要部分:

BuildingsAdapter.java

class BuildingsAdapter extends ArrayAdapter<BaseRow> {

    @Override
    public int getItemViewType(int position) {

        return getItem(position).getType();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        int type = getItemViewType(position);

        View row = convertView;

        switch (type) {
        case BaseRow.QUEUE_ROW: {
            QueueRowViewHolder holder = new QueueRowViewHolder();
            if (row == null) {
                LayoutInflater inflater = (LayoutInflater) getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.queue_row, parent, false);

                holder.qItemImageView_ = (ImageView) row
                        .findViewById(R.id.qItemImageView);
                (..........)
                row.setTag(holder);
            } else {
                holder = (QueueRowViewHolder) row.getTag();
            }
            final QueueRow queueRow = (QueueRow) getItem(position);

            if (queueRow != null) {
                if (holder.qItemImageView_ != null) {
                    (..........)
                    holder.qItemImageView_.setImageBitmap(bm);
                }
                if (holder.qItemNameTextView_ != null) {
                    holder.qItemNameTextView_.setText(queueRow.getName());
                }
                if (holder.qProgressBar_ != null) {
                    holder.qProgressBar_.setProgress(queueRow.getProgress());
                }
                if (holder.qLevelTextView_ != null) {
                    holder.qLevelTextView_.setText(queueRow.getLevel());
                }
                if (holder.qTimeTextView_ != null) {
                    holder.qTimeTextView_.setText(queueRow.getTime());
                }
            }
            return row;
        }
        case BaseRow.ROW: {
            RowViewHolder holder = new RowViewHolder();
            if (row == null) {
                LayoutInflater inflater = (LayoutInflater) getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.row, parent, false);

                (..........)
                row.setTag(holder);
            } else {
                holder = (RowViewHolder) row.getTag();
            }

            final Row element = (Row) getItem(position);

            row.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    Toast.makeText(mainActivity_, element.getDescription(),
                            Toast.LENGTH_LONG).show();
                }
            });

            if (element != null) {

                if (holder.itemImageView_ != null) {
                    (..........)
                    holder.itemImageView_.setImageBitmap(bm);
                }
                if (holder.nameTextView_ != null)
                    holder.nameTextView_.setText(element.getName());
                if (holder.levelTextView_ != null)
                    holder.levelTextView_.setText(String.valueOf(element
                            .getLevel()));
                if (holder.woodTextView_ != null)
                    holder.woodTextView_.setText(String.valueOf(element
                            .getWoodPrice()));
                if (holder.ironTextView_ != null)
                    holder.ironTextView_.setText(String.valueOf(element
                            .getIronPrice()));
                if (holder.stoneTextView_ != null)
                    holder.stoneTextView_.setText(String.valueOf(element
                            .getStonePrice()));
                if (holder.goldTextView_ != null)
                    holder.goldTextView_.setText(String.valueOf(element
                            .getGoldPrice()));
                if (holder.timeTextView_ != null)
                    holder.timeTextView_.setText(element.getTime());
                if (holder.actionButton_ != null && element.getAction() != null) {
                    holder.actionButton_
                            .setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {

                                    BuildingsAdapter.this.mainActivity_.webView_
                                            .loadUrl("javascript:"
                                                    + element.getAction());
                                }
                            });
                } else {
                    holder.actionButton_.setEnabled(false);
                }
            }
            return row;
        }
        case BaseRow.HEADER_ROW: {
            HeaderViewHolder holder = new HeaderViewHolder();
            if (row == null) {
                LayoutInflater inflater = (LayoutInflater) getContext()
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.header_row, parent, false);

                HeaderRow headerRow = (HeaderRow) getItem(position);

                holder.nameTextView_ = (TextView) row
                        .findViewById(R.id.categoryNameTextView);
                holder.nameTextView_.setText(headerRow.getName());

                row.setTag(holder);
            } else {
                holder = (HeaderViewHolder) row.getTag();
            }
            return row;
        }
        }
        return null;
    }

    static class QueueRowViewHolder {

        ImageView qItemImageView_;
        TextView qItemNameTextView_;
        ProgressBar qProgressBar_;
        TextView qLevelTextView_;
        TextView qTimeTextView_;
        int position_;
    }

    static class RowViewHolder {

        ImageView itemImageView_;
        TextView nameTextView_;
        TextView levelTextView_;
        TextView woodTextView_;
        TextView ironTextView_;
        TextView stoneTextView_;
        TextView goldTextView_;
        Button actionButton_;
        TextView timeTextView_;
    }

    static class HeaderViewHolder {

        TextView nameTextView_;
    }
}

你有什么想法吗?这里的确切日志:

06-18 22:40:35.899: E/AndroidRuntime(2015): FATAL EXCEPTION: main
06-18 22:40:35.899: E/AndroidRuntime(2015): java.lang.ClassCastException: kks.imperioid.BuildingsAdapter$HeaderViewHolder
06-18 22:40:35.899: E/AndroidRuntime(2015):     at kks.imperioid.BuildingsAdapter.getView(BuildingsAdapter.java:146)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.widget.AbsListView.obtainView(AbsListView.java:1589)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.widget.ListView.makeAndAddView(ListView.java:1795)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.widget.ListView.fillDown(ListView.java:718)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.widget.ListView.fillGap(ListView.java:689)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4173)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:2610)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.widget.ListView.onTouchEvent(ListView.java:3682)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.view.View.dispatchTouchEvent(View.java:3885)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1750)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1135)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1734)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2216)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1887)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.os.Looper.loop(Looper.java:130)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at java.lang.reflect.Method.invokeNative(Native Method)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at java.lang.reflect.Method.invoke(Method.java:507)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-18 22:40:35.899: E/AndroidRuntime(2015):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

在您的适配器中,您需要覆盖getViewTypeCount()并提供一个实现,该实现返回将从View返回的不同类型getView()的数量(在您的情况下:3) :

    @Override
    public int getViewTypeCount() {
        return 3;
    }

答案 1 :(得分:2)

你不记得这个:

@Override
public int getViewTypeCount() {
    return 3;
}

在您的适配器