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

时间:2017-03-18 14:58:34

标签: java android

我正在使用聊天应用程序,但我无法找到我的9个传入邮件的补丁图像到左边和外部邮件到右边。 在使用LayoutParams时,我遇到了这个例外:

  

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

我的适配器类:

public class MessageAdapter extends ResourceCursorAdapter {
    private final int backgroundDrawableIn, backgroundDrawableOut
    private final MessageListActivity mActivity;
    private String displayName = null;
    private final int textSize, textColor;
    private final boolean convertNCR;
    private final boolean showEmoticons;

    private static class ViewHolder {
        TextView tvBody;
        TextView tvDate;
        ImageView ivPhoto;
        View vRead;
        public View vPending;
        // public View vLayout;
        public RelativeLayout vLayout;
    }
    public MessageAdapter(final MessageListActivity c, final Uri u) {
        super(c, R.layout.messagelist_item, getCursor(c.getContentResolver(), u), true);
        mActivity = c;
        backgroundDrawableIn = PreferencesActivity.getBubblesIn(c);
        backgroundDrawableOut = PreferencesActivity.getBubblesOut(c);
        textSize = PreferencesActivity.getTextsize(c);
        textColor = PreferencesActivity.getTextcolor(c);
        convertNCR = PreferencesActivity.decodeDecimalNCR(c);
        showEmoticons = PreferencesActivity.showEmoticons(c);
        int threadId = -1;
        if (u == null || u.getLastPathSegment() == null) {
            threadId = -1;
        } else {
            threadId = Integer.parseInt(u.getLastPathSegment());
        }
        final Conversation conv = Conversation.getConversation(c, threadId, false);

        // Address
        String address = null;

        //Name
        String name = null;
        if (conv == null) {
            address = null;
            name = null;
            displayName = null;
        } else {
            final Contact contact = conv.getContact();
            address = contact.getNumber();
            name = contact.getName();
            displayName = contact.getDisplayName();
        }
    }

    @Override
    public final void bindView(final View view, final Context context, final Cursor cursor) {
        final Message m = Message.getMessage(context, cursor);

        ViewHolder holder = (ViewHolder) view.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.tvBody = (TextView) view.findViewById(R.id.body);
            holder.tvDate = (TextView) view.findViewById(R.id.date);
            holder.ivPhoto = (ImageView) view.findViewById(R.id.picture);
            holder.vRead = view.findViewById(R.id.read);
            holder.vPending = view.findViewById(R.id.pending);
            //holder.vLayout = view.findViewById(R.id.layout);
            holder.vLayout=(RelativeLayout)view.findViewById(R.id.layout);
            view.setTag(holder);
        }

        if (textSize > 0) {
            holder.tvBody.setTextSize(textSize);
        }
        final int col = textColor;
        if (col != 0) {
            holder.tvBody.setTextColor(col);
            holder.tvDate.setTextColor(col);
        }
        int t = m.getType();

        String subject = m.getSubject();
        if (subject == null) {
            subject = "";
        } else {
            subject = ": " + subject;
        }
        // incoming / outgoing / pending
        int pendingvisability = View.GONE;
        switch (t) {
            case Message.SMS_DRAFT:
                pendingvisability = View.VISIBLE;
            case Message.SMS_OUT:
            case Message.MMS_OUT:
                try {
                    holder.vLayout.setBackgroundResource(backgroundDrawableOut);
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.vLayout.getLayoutParams();
                    layoutParams.gravity = Gravity.LEFT;
                    holder.vLayout.setLayoutParams(layoutParams);
                    LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams) holder.tvBody.getLayoutParams();
                    layoutParams.gravity = Gravity.LEFT;
                    holder.tvBody.setLayoutParams(lp);
                    LinearLayout.LayoutParams lpp=(LinearLayout.LayoutParams) holder.tvDate.getLayoutParams();
                    layoutParams.gravity = Gravity.LEFT;
                    holder.tvDate.setLayoutParams(lpp);

                } catch (OutOfMemoryError e) {
                    Log.e(TAG, "OOM while setting bg", e);
                }
                break;
            case Message.SMS_IN:
            case Message.MMS_IN:
            default:
                try {
                    holder.vLayout.setBackgroundResource(backgroundDrawableIn);
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.vLayout.getLayoutParams();
                    layoutParams.gravity = Gravity.RIGHT;
                    holder.vLayout.setLayoutParams(layoutParams);

                    LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams) holder.tvBody.getLayoutParams();
                    layoutParams.gravity = Gravity.RIGHT;
                    holder.tvBody.setLayoutParams(lp);

                    LinearLayout.LayoutParams lpp=(LinearLayout.LayoutParams) holder.tvDate.getLayoutParams();
                    layoutParams.gravity = Gravity.RIGHT;
                    holder.tvDate.setLayoutParams(lpp);



                } catch (OutOfMemoryError e) {
                    Log.e(TAG, "OOM while setting bg", e);
                }
                holder.vPending.setVisibility(View.GONE);
                break;
        }
        holder.vPending.setVisibility(pendingvisability);
        if (m.getRead() == 0) {
            holder.vRead.setVisibility(View.VISIBLE);
        } else {
            holder.vRead.setVisibility(View.INVISIBLE);
        }

        final long time = m.getDate();
        holder.tvDate.setText(Conversation_list.getDate(context, time));

        final Bitmap pic = m.getPicture();
        if (pic != null) {
            if (pic == Message.BITMAP_PLAY) {
                holder.ivPhoto.setImageResource(R.drawable.mms_play_btn);
            } else {
                holder.ivPhoto.setImageBitmap(pic);
            }
            holder.ivPhoto.setVisibility(View.VISIBLE);
            final Intent i = m.getContentIntent();
            holder.ivPhoto.setOnClickListener(VSMS.getOnClickStartActivity(context, i));

        } else {
            holder.ivPhoto.setVisibility(View.GONE);
            holder.ivPhoto.setOnClickListener(null);
        }

        CharSequence text = m.getBody();
        if (text == null) {
            holder.tvBody.setVisibility(View.INVISIBLE);
        } else {
            if (convertNCR) {
                text = Converter.convertDecNCR2Char(text);
            }
            if (showEmoticons) {
                text = SmileyParser.getInstance(context).addSmileySpans(text);
            }
            holder.tvBody.setText(text);
            holder.tvBody.setVisibility(View.VISIBLE);

        }
    }
}

XML

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

<View
    android:id="@+id/read"
    android:background="#007e88"
    android:layout_height="wrap_content"
    android:layout_width="5dip"
    android:layout_marginRight="1dip"
    android:layout_alignParentStart="true" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:orientation="vertical"
    android:id="@+id/layout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/body"
        android:text="@+id/body"
        android:layout_gravity="right"
        android:layout_below="@+id/addr"
        android:singleLine="false"
        android:autoLink="all"
        />
    <ImageView
        android:id="@+id/pending"
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:layout_marginLeft="2dip"
        android:layout_toRightOf="@+id/date"
        android:src="@drawable/ic_sms_mms_pending"
        android:visibility="gone"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_below="@id/body"
        android:layout_gravity="right"
        android:layout_height="wrap_content"
        android:id="@+id/date"
        android:text="@+id/date"
        android:singleLine="true"/>




</RelativeLayout>
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/picture"
    android:visibility="gone"

    android:maxWidth="178dip"
    android:maxHeight="178dip"
    android:adjustViewBounds="true"
    android:background="@android:drawable/picture_frame"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

尝试使用relativelayout而不是线性布局。您必须使用父类。

答案 1 :(得分:0)

您的异常是由以下行引起的:

LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.vLayout.getLayoutParams();

vLayoutRelativeLayout的一个实例,因此,当您致电getLayoutParams()时,它会返回RelativeLayout.LayoutParams而不是LinearLayout.LayoutParams的实例。鉴于此事实,当您将返回的值转换为LinearLayout params时,您将创建一个例外,因为RelativeLayout.LayoutParamsLinearLayout.LayoutParams相比是一个不同的类。要解决您的问题,请将每个LinearLayout.LayoutParams替换为RelativeLayout.LayoutParams

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.vLayout.getLayoutParams();