繁体   English   中英

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

[英]java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

我要使用聊天应用程序,但无法找到传入消息左侧的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>

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

您的异常是由这样的行引起的:

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

vLayoutRelativeLayout一个实例,因此,当您调用getLayoutParams() ,它将返回RelativeLayout.LayoutParams一个实例,而不是LinearLayout.LayoutParams 鉴于此事实,当您将返回值转换为LinearLayout params ,由于RelativeLayout.LayoutParams是与LinearLayout.LayoutParams不同的类,因此将创建异常。 要解决您的问题,请使用RelativeLayout.LayoutParams替换每个LinearLayout.LayoutParams

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.AbsListView $ LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.FrameLayout $ LayoutParams java.lang.ClassCastException:android.view.ViewGroup$LayoutParams 不能转换为 android.widget.RelativeLayout$LayoutParams java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.FrameLayout Android java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.v7.widget.RecyclerView$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.support.v7.widget.RecyclerView $ LayoutParams java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.constraint.ConstraintLayout$LayoutParams ClassCastException:java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.support.v4.widget.DrawerLayout ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为com.android.internal.widget.ActionBarOverlayLayout $ LayoutParams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM