Android - ClassCastException - 从LinearLayout到AbsListView的LayoutParams

Win*_*ron 16 android android-listview

我有一个列表适配器,我正在尝试为(item.xml)设置自定义xml,如果我不使用通胀(通过应用程序上下文直接识别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;
Run Code Online (Sandbox Code Playgroud)

[..]

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)
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

Jul*_*les 36

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

要轻松扩展:您正在创建控件,然后将其添加到LinearLayout中.此时,为它分配LinearLayout $ LayoutParams对象并将其设置为其布局参数.然后将其拉出线性布局并将其放入列表视图中,该视图不理解LinearLayout $ LayoutParams.

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

(另一个不相关的提示:你正在膨胀你的布局,然后在convertView!= null时丢弃它;这将导致慢速设备上的滚动性能不佳.只有在你真正需要它时才执行通胀.)

  • 谢谢你@Jules,它有助于理解这个问题!如果你仍想使用tv对象,你可以做的另一件事是将它的布局参数设置为AbsListView.LayoutParams的一个实例,即:tv.setLayoutParams(new AbsListView.LayoutParams(width,height); (4认同)
  • 这个答案有误导性.OP可能正在返回电视而不是排.如果OP更改了返回行的方法,则不会发生类强制转换异常.如果这是您想要的,没有理由不将行的TextView包装在LinearLayout中. (3认同)

Mar*_*iol 20

你可能正在回电视而不是排

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!
Run Code Online (Sandbox Code Playgroud)


小智 5

您是否尝试使用布局inflater来扩充静态xml布局?像这样:

LayoutInflater li = LayoutInflater.from(context);
View rootView = li.inflate(R.layout.item, null);
Run Code Online (Sandbox Code Playgroud)