java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams是错误
我正在使用BaseExpandableListAdapter,如下所示。而不是传递列表,我直接传递一个视图作为子数据。并且子计数总是返回1.因为每个标题只有一个视图。
public class TaxAdapter extends BaseExpandableListAdapter{
private Context _context;
private ArrayList<String> _listDataHeader;
private HashMap<String, View> _listDataChild;
public TaxAdapter(Context context, ArrayList<String> taxListDataHeader,
HashMap<String, View> taxListDataChild) {
this._context = context;
this._listDataHeader = taxListDataHeader;
this._listDataChild = taxListDataChild;
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition));
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LinearLayout earningsContainer = new LinearLayout(_context);
LayoutInflater inflator = ((Activity)_context).getLayoutInflater();
convertView = inflator.inflate(R.layout.row_tax_explv_title,
earningsContainer, false);
((TextView) convertView.findViewById(R.id.explvTaxTitleText)).setText(headerTitle);
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
return (View)getChild(groupPosition, childPosition);
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
并且从活动传递的数据如下 -
ArrayList<String> TaxListDataHeader = new ArrayList<String>();
HashMap<String, View> TaxListDataChild = new HashMap<String, View>();
TaxListDataHeader.add("Movie Shows");
TaxListDataChild.put("Movie Shows", moviesContainerView);
TaxListDataHeader.add("Theatre Shows");
TaxListDataHeader.add("Theatre Shows", theatreContainerView);
movieContainerView和theareContainerView是两个自定义的膨胀线性布局。像这样
LayoutInflater inflator = ((Activity) context).getLayoutInflater();
View row = inflator.inflate(R.layout.row_movie_data,
movieMainContainer, false);
((TextView) row.findViewById(R.id.txtEmpInfoPopUpTitle)).setText(key);
((TextView) row.findViewById(R.id.txtEmpInfoPopUpValue)).setText(value);
return row;
我收到标题中的错误,但日志没有指定任何文件名。 请提出错误发生的位置。请建议可以做些什么。
修改 这是堆栈跟踪
07-17 22:53:57.043: E/AndroidRuntime(5074): FATAL EXCEPTION: main
07-17 22:53:57.043: E/AndroidRuntime(5074): Process: com.synthesize.paysal, PID: 5074
07-17 22:53:57.043: E/AndroidRuntime(5074): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.ListView.measureScrapChild(ListView.java:1183)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.ListView.onMeasure(ListView.java:1149)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1226)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.ScrollView.onMeasure(ScrollView.java:326)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-17 22:53:57.043: E/AndroidRuntime(5074): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.Choreographer.doFrame(Choreographer.java:544)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.os.Handler.handleCallback(Handler.java:733)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.os.Handler.dispatchMessage(Handler.java:95)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.os.Looper.loop(Looper.java:136)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-17 22:53:57.043: E/AndroidRuntime(5074): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 22:53:57.043: E/AndroidRuntime(5074): at java.lang.reflect.Method.invoke(Method.java:515)
07-17 22:53:57.043: E/AndroidRuntime(5074): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-17 22:53:57.043: E/AndroidRuntime(5074): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-17 22:53:57.043: E/AndroidRuntime(5074): at dalvik.system.NativeStart.main(Native Method)
修改 我试图用这样的列表视图包装线性布局,
TaxListDataChild.put(“剧场秀”,(新的 ListView控件(上下文))。addView(theatrerowContainer, LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
但似乎第二个参数是无效的。
我正在使用其他线性布局行填充xml中的“new Linearlayout”(earningsContainer)。
for (int i = 0; i < taxMainObject.getTaxEarnings().size(); i++) {
earningsContainer.addView(getTaxEarningsRow(taxMainObject
.getTaxEarnings().get(i)));
totalEarnings += taxMainObject.getTaxEarnings().get(i)
.getTotalAmount();
}
earningsContainer.addView(getTaxTotalRow("GROSS TOTAL SALARY",
totalEarnings + ""));
并且行的xml是 -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal"
android:background="#ffeeeeee" >
<TextView
android:id="@+id/txtTotalText"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textAppearance="@android:attr/textAppearanceSmall"
android:textColor="#736F6E"
android:textSize="14sp"
android:textStyle="bold|italic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right|center_vertical"
android:orientation="horizontal"
android:padding="2dp" >
<TextView
android:id="@+id/txtTotalValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="@android:attr/textAppearanceSmall"
android:textColor="#736F6E"
android:gravity="right"
android:textSize="14sp"
android:layout_weight="1"
android:textStyle="italic" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
我无法发表评论,但请尝试将其投射到android.widget.AbsListView.LayoutParams,然后将其更改为强制转换为android.widget.LinearLayout.LayoutParams(不要问我为什么需要更多比1布局params类),但是如果在内部android文件中你可能必须找到执行转换的方法并用你自己的
覆盖它