简体   繁体   English

java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.AbsListView $ LayoutParams

[英]java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

i am using a BaseExpandableListAdapter as follows. 我正在使用BaseExpandableListAdapter如下。 where instead of passing a list, i am directly passing a View to be a child data. 我没有传递列表,而是直接传递一个View作为子数据。 and the child count is always returned as 1. as there is only one view per heading. 并且子项计数始终返回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;
    }

}

and the data being passed from the activity is as follows - 活动中传递的数据如下-

    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 and theareContainerView are the two custom inflated linear layouts.like this 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;

i am getting the error as in the title, but the logs do not specify any file name as such. 我在标题中遇到错误,但是日志没有这样指定任何文件名。 Please suggest where the error might be occuring. 请指出可能发生错误的位置。 Please suggest what can be done. 请提出可以做什么。

EDIT: This is the stack trace 编辑:这是堆栈跟踪

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)

EDIT: i have tried to wrap the linear layout with a list view like this, 编辑:我试图用这样的列表视图包装线性布局,

TaxListDataChild.put("Theatre Show", (new ListView(context)).addView(theatrerowContainer, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); TaxListDataChild.put(“ Theatre Show”,(new ListView(context))。addView(theatrerowContainer,LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

but it seems that the second parameter is void. 但似乎第二个参数为空。

I am populating the "new Linearlayout" (earningsContainer) in the xml like this with other linear layout rows. 我在xml中使用其他线性布局行填充“新的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 + ""));

and the xml for the rows is - 行的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>

Although this is not related to your java but i still got same error. 虽然这与您的Java不相关,但是我仍然遇到相同的错误。 I had "extends AppCompatActivity" in my class but when i replaced it with "Extends Activity" Error was resolved. 我在班级中拥有“扩展AppCompatActivity”,但是当我将其替换为“扩展活动”时,错误已解决。 Hope this is helpful for someone else, as i was stuck for three hours on this. 希望这对其他人有帮助,因为我被困了三个小时。 Happy to help 乐意效劳

android.widget.ListView$LayoutParams is subclass of android.widget.ViewGroup$LayoutParams while android.widget.AbsListView is also subclass of same one ie sibling relationship hence type casting will throw an exception. android.widget.ListView $ LayoutParams是android.widget.ViewGroup $ LayoutParams的子类,而android.widget.AbsListView也是同一个子类,即同级关系,因此类型转换将引发异常。 Reference 参考

http://developer.android.com/reference/android/widget/AbsListView.LayoutParams.html http://developer.android.com/reference/android/widget/AbsListView.LayoutParams.html

This suggests you can't pass a view directly as a child to listview adapter instead try passing Hashmap and inflate the view dynamically. 这表明您不能直接将视图作为子级传递给listview适配器,而应尝试传递Hashmap并动态膨胀视图。 Try this : 尝试这个 :

   @Override
    public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
 }

Where listDataHeader is a hashmap passed to adapter like 其中listDataHeader是传递给适配器的哈希图,例如

public ExpandableListAdapter(Context context, List<String> listDataHeader,
        HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

(Passed HashMap Instead of View) (通过HashMap而不是View)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.widget.AbsListView $ LayoutParams - Android java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.FrameLayout - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.v7.widget.RecyclerView$LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams 不能转换为 android.support.constraint.ConstraintLayout$LayoutParams - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams ClassCastException:java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.support.v4.widget.DrawerLayout - ClassCastException : java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.FrameLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为com.android.internal.widget.ActionBarOverlayLayout $ LayoutParams - ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to com.android.internal.widget.ActionBarOverlayLayout$LayoutParams java.lang.ClassCastException:android.view.ViewGroup$LayoutParams 不能转换为 android.widget.RelativeLayout$LayoutParams - java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams android.widget.FrameLayout $ LayoutParams无法转换为android.widget.AbsListView $ LayoutParams - android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM