Android-ClassCastException-on从LinearLayout到AblistView的布局参数

Android-ClassCastException-on从LinearLayout到AblistView的布局参数,android,android-listview,Android,Android Listview,我有一个列表适配器,我正试图为它设置自定义xml(item.xml) 如果我不使用inflation(通过应用程序上下文直接识别textview),它将非常有效。。但是我的listview不能被主题化 public View getView(int position, View convertView, ViewGroup parent) { //System.gc(); TextView tv; LayoutInflater inflater

我有一个列表适配器,我正试图为它设置自定义xml(item.xml) 如果我不使用inflation(通过应用程序上下文直接识别textview),它将非常有效。。但是我的listview不能被主题化

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;
[……]

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文件如下所示:

<?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>

使用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);

另外,如果可能,不要在代码中手动进行垃圾收集。

是否尝试使用布局充气器充气静态xml布局?像这样:

LayoutInflater li = LayoutInflater.from(context);
View rootView = li.inflate(R.layout.item, null);

该问题是由于将从适配器返回的控件包装在linearlayout中引起的。从布局文件中丢失LinearLayout(只需将TextView作为根),我认为它应该可以工作

展开:创建控件,然后将其添加到线性布局中。此时,将为其分配LinearLayout$LayoutParams对象,并将其设置为布局参数。然后将其从线性布局中拉出并放入列表视图中,该视图不理解LinearLayout$LayoutParams


或者,从getView返回行,而不是返回tv(我认为您现在正在这样做,因为您已经从引用的代码中删除了返回行)


(另一个不相关的提示:您正在扩大布局,然后在convertView!=null时放弃布局;这将导致低速设备上的滚动性能不佳。只有在您实际需要时才执行扩大。)

您可能返回的是电视而不是行

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!
这是正确的:

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;
}

我也遇到了同样的问题,我使用了以下代码:

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;
}

没有,还是有同样的错误。如果我将tv=newtextview(mContext.getApplicationContext());在if语句中,只有这样它才能工作,但不能呈现所需的xml文件..是的!它起作用了。。但正如你所说,滚动有点不稳定。。我如何在不进行通货膨胀的情况下使其平稳?比如,如何在不膨胀列表的情况下获得自定义布局?我不明白丢弃的部分。。它是如何被丢弃的?此列表的uri来源于mediastore..感谢@Jules的介绍,这有助于理解此问题!如果仍然要使用tv对象,可以做的另一件事是将其布局参数设置为AbsListView.LayoutParams的实例,即:tv.setLayoutParams(新的AbsListView.LayoutParams(宽度、高度);“或者,从getView返回行,而不是(我想你现在正在做,因为你已经从引用的代码中删除了返回线)返回电视。”这很有魅力。谢谢。这个答案有误导性。OP可能返回tv而不是row。如果OP更改了返回row的方法,那么类强制转换异常将不会发生。如果这是您想要的,那么没有理由不将该行的TextView包装成线性布局。@ditkin--确实,这就是为什么我的答案包含建议“或者,从getView返回行,而不是…返回电视”。另一方面,对于OP had应用程序,
LinearLayout
是不必要的,因此最好不要为其应用程序显示的每个列表视图项创建一个。任何人都可以帮助吗?这甚至不会编译。变量视图未声明。