Android - ClassCastException - LayoutParams 从 LinearLayout 到 AbsListView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11492678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - ClassCastException - on LayoutParams from LinearLayout to AbsListView
提问by Wingston Sharon
I have a list adaptor that i'm trying to set a custom xml for (item.xml) if i dont use inflation (direct identifying the textview through an application context), it works perfectly.. but then my listview can't be themed..
我有一个列表适配器,如果我不使用通货膨胀(通过应用程序上下文直接识别文本视图),我正在尝试为 (item.xml) 设置自定义 xml,它工作得很好..但是我的列表视图不能主题..
public View getView(int position, View convertView, ViewGroup parent) {
//System.gc();
TextView tv;
LayoutInflater inflater = getLayoutInflater();
View row = View.inflate(mContext,R.layout.item,null); //.inflate(R.layout.item, parent, false); doesnt work either..
String id = null;
if (convertView == null) {
tv = (TextView) row.findViewById(R.id.TextView01);;
} else
tv = (TextView) convertView;
[..]
[..]
in logcat
I am getting this..
在logcat
我得到这个..
07-15 19:45:51.710: ERROR/AndroidRuntime(20185): FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.ListView.setupChild(ListView.java:1827)
at android.widget.ListView.makeAndAddView(ListView.java:1794)
at android.widget.ListView.fillDown(ListView.java:688)
my item.xml file is as follows:
我的 item.xml 文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:gravity="left|center"
android:layout_width="fill_parent"
android:paddingBottom="15px"
android:background="#fff200"
android:paddingTop="15px"
android:paddingLeft="15px">
<TextView android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40px"
android:textStyle="bold"
android:layout_marginLeft="20px"
android:textColor="#0099CC">
</TextView>
</LinearLayout>
回答by Jules
The problem is caused by wrapping the control you're returning from the adapter in a linearlayout. Lose the LinearLayout from your layout file (just put the TextView as the root) and I think it should work.
该问题是由于将您从适配器返回的控件包装在线性布局中引起的。从您的布局文件中丢失 LinearLayout(只需将 TextView 作为根),我认为它应该可以工作。
To expand slighly: you're creating your control, and then adding it into a LinearLayout. At this point a LinearLayout$LayoutParams object is allocated for it and set as its layout params. You then pull it out of the linear layout and put it into your list view, which doesn't understand the LinearLayout$LayoutParams.
稍微扩展一下:您正在创建控件,然后将其添加到 LinearLayout 中。此时会为其分配一个 LinearLayout$LayoutParams 对象并将其设置为它的布局参数。然后将其从线性布局中拉出并放入不理解 LinearLayout$LayoutParams 的列表视图中。
Alternatively, return row from getView rather than (as I presume you are doing at the moment, as you have cut the return line out of your quoted code) returning tv.
或者,从 getView 返回行而不是(正如我假设您目前正在做的那样,因为您已经从引用的代码中删除了返回行)返回 tv.
(Another, unrelated hint: you are inflating your layout and then discarding it when convertView != null; this will result in poor scrolling performance on slow devices. Only perform the inflation if you're actually going to need it.)
(另一个不相关的提示:您正在膨胀您的布局,然后在 convertView != null 时丢弃它;这将导致在慢速设备上滚动性能不佳。只有在您确实需要它时才执行膨胀。)
回答by Marco Bettiol
you probably are returning the tv instead of row
你可能正在返回电视而不是行
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
LayoutInflater inflater = getLayoutInflater();
View row = View.inflate(mContext,R.layout.item,null);
String id = null;
if (convertView == null) {
tv = (TextView) row.findViewById(R.id.TextView01);;
} else
tv = (TextView) convertView;
}
return row; // returning tv brings to ClassCast!
回答by Soumyajyoti Saha
Did you try to use the layout inflater to inflate the static xml layout? Like this:
您是否尝试使用布局充气器来充气静态 xml 布局?像这样:
LayoutInflater li = LayoutInflater.from(context);
View rootView = li.inflate(R.layout.item, null);
回答by Hadi Note
I had the same problem and i used these codes :
我遇到了同样的问题,我使用了这些代码:
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = G.layoutInflater.inflate(R.layout.list_lay_cities_markets, null);
// your work
notifyDataSetChanged();
return convertView;
}
回答by azgolfer
A proper way to work with ArrayAdapter's getView for your code is this:
为您的代码使用 ArrayAdapter 的 getView 的正确方法是:
public View getView(int position, View convertView, ViewGroup parent) {
//System.gc(); // Should not manually call gc
View row;
LayoutInflater inflater = getLayoutInflater();
if (convertView == null) {
row = inflater.inflate(R.layout.item, parent, false);
} else
row = convertView;
TextView tv = (TextView) row.findViewById(R.id.TextView01);
Also, do not manually garbage collect in your code if possible.
此外,如果可能,不要在您的代码中手动进行垃圾回收。
回答by Victor Maldonado
This is the correct:
这是正确的:
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
final ViewHolder holder;
if(view==null){
vi = inflater.inflate(R.layout.listview_row_myvideos,null);
holder = new ViewHolder();
holder.tv = (TextView) vi.findViewById(R.id.TextView01);
vi.setTag(holder);
}else{
holder = (ViewHolder)vi.getTag();
}
return vi;
}
public class ViewHolder{
TextView tv;
}