[英]java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$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();
vLayout
是RelativeLayout
一個實例,因此,當您調用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.