2

我膨胀了

linear = (LinearLayout) findViewById(R.id.layout_content);
    linear.setVisibility(View.VISIBLE);
LayoutInflater liInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslist, null));
    linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslistfv, null));

这是我的列表视图

btnmore = (Button)findViewById(R.id.btn_more);
getListView().addFooterView(btnmore);
lv.setAdapter(adapter);

我想第二次充气但失败了。

但是我可以第一次充气,这是

linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslist, null));

我收到此错误的问题是什么?

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
4

3 回答 3

1

Try changing this:

linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslist,
      null));

To this:

linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslist,
      linear));
于 2012-05-04T07:02:59.157 回答
0

我有类似的情况 - list_view.setAdapter(); 上有异常“java.lang.ClassCastException:android.widget.RelativeLayout$LayoutParams”;

我已经设法通过对页脚和列表项使用相同的布局来解决这个问题。下面的代码演示了如何在页脚和项目中使用布局“listview_row”。

这是“listview_row.xml”的内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/hs_line" 
android:layout_alignParentLeft ="true"
android:layout_width="fill_parent" 
android:layout_height="wrap_content">

<!-- TextView for list items -->
<TextView android:id="@+id/hs_line_textview" 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_alignParentLeft="true"       
    />

<!-- Button for footer -->
<Button android:id="@+id/hs_line_footer_action" 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:visibility="gone"
    android:layout_alignParentLeft="true"       
/>
</RelativeLayout>

页脚以这种方式初始化:

View v = View.inflate(pager.Activity, R.layout.listview_row,  null);
View footer = (View) v.findViewById(R.id.listview_line);

//footer doesn't use TextView, it uses Button only. So, we hide TextView.
footer.findViewById(R.id.hs_line_textview).setVisibility(View.GONE); 
footer.findViewById(R.id.hs_line_footer_action).setVisibility(View.VISIBLE)
...
list_view.addFooterView(footer);

MyAdapter<Item> adapter = new MyAdapter(context, getListItems());
list_view.setAdapter(adapter);

适配器:

public class MyAdapter<T> extends ArrayAdapter<T> {
private final ArrayList<T> _List;
private final LayoutInflater _Inflater;

public MyAdapter(Context context, ArrayList<T> srcList) {
    super(context, 0, srcList);
    _Inflater = LayoutInflater.from(context);
    _List = srcList;
}

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {    
            convertView = _Inflater.inflate(R.layout.listview_row, null);
            holder = new ViewHolder();

        //item doesn't use Button, it uses TextView only   
        //Button is hidden by default (see xml)
            holder.TextView = (TextView) convertView.findViewById(R.id.hs_line_textview);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //item initialization 
        .....
   }

这不是理想的解决方案 - 当然只有解决方法。

于 2012-05-15T03:02:34.263 回答
0

i think It's not because of the line you pointed out..

Did you call getLayoutParams() anywhere in your code?

Whenver you call getLayoutParams(), you show typecase to parent layoutparams.

Like, if your ListView's parent is LinearLayout then,

LinearLayout.LayoutParams params=(LinearLayout.LayoutParams) listView.getLayoutParams();

If your ListView's parent is RelativeLayout then,

RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams) listView.getLayoutParams();
于 2012-05-04T07:02:58.407 回答