java.lang.ClassCastException:android.widget.RelativeLayout$LayoutParams不能强制转换为android.widget.AblistView$LayoutParams?

java.lang.ClassCastException:android.widget.RelativeLayout$LayoutParams不能强制转换为android.widget.AblistView$LayoutParams?,android,Android,我的应用程序面临崩溃。类强制转换异常仅发生420 X 800设备解析(KitKat 19 API)。相同的代码在其他分辨率的设备上运行。为什么我只得到420 x 800的设备分辨率 这是我的密码 我在可扩展列表视图中打开网格视图,并动态设置网格视图的高度 public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context _context;

我的应用程序面临崩溃。类强制转换异常仅发生420 X 800设备解析(KitKat 19 API)。相同的代码在其他分辨率的设备上运行。为什么我只得到420 x 800的设备分辨率

这是我的密码

我在可扩展列表视图中打开网格视图,并动态设置网格视图的高度

     public class ExpandableListAdapter extends BaseExpandableListAdapter
        {

            private Context _context;
            private ArrayList<Object> _listDataHeader = new ArrayList<Object>(); // header titles
            // child data in format of header title, child title
            private HashMap<Object, ArrayList<Object>> _listDataChild = new HashMap<Object, ArrayList<Object>>();
            public ExpandableListAdapter(Context context, ArrayList<Object> listDataHeader,
                                         HashMap<Object, ArrayList<Object>> listChildData) {
                this._context = context;
                this._listDataHeader = listDataHeader;
                this._listDataChild = listChildData;

            }

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

            @Override
            public long getChildId(int groupPosition, int childPosition)
            {
                return childPosition;
            }
            public class ViewHolder
            {

                public TextView _groupTxt = null;
                public GridView _subCatGrid;
                public GridAdapter _gridAdapter;
                public RelativeLayout _subCatll;
                public ImageView _catIcon;
                public TextView _catCount;

            }

            @Override
            public View getChildView(int groupPosition, final int childPosition,
                                     boolean isLastChild, View convertView, ViewGroup parent)
            {
                if(_listDataHeader.size()>0)
                {
                    try {
                        CategoryBean catBean = (CategoryBean) _listDataHeader.get(groupPosition);

                        ArrayList<SubCatBean> beanArrayList = catBean.getChildArrayList();


                        ViewHolder _viewHolder;

                        if (convertView == null) {
                            _viewHolder = new ViewHolder();
                            LayoutInflater infalInflater = (LayoutInflater) this._context
                                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            convertView = infalInflater.inflate(R.layout.cat_child_row, null);
                            _viewHolder._subCatGrid = (GridView) convertView.findViewById(R.id.sub_cat_grid);
                            _viewHolder._subCatll = (RelativeLayout) convertView.findViewById(R.id.subCatLL);


                            convertView.setTag(_viewHolder);
                        } else {
                            _viewHolder = (ViewHolder) convertView.getTag();
                        }

                        System.out.println("group position" + groupPosition);
                        _viewHolder._gridAdapter = new GridAdapter(getActivity(), beanArrayList);
                        _viewHolder._subCatGrid.setAdapter(_viewHolder._gridAdapter);
                        _viewHolder._gridAdapter.notifyDataSetChanged();

                        //dynamic set hight and width from runtime
                        _viewHolder._subCatll.measure(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
                        int height = _viewHolder._subCatll.getMeasuredHeight();
                        _viewHolder._subCatll.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, height));    
                    } catch (Exception e) {

                    }
                }

                return convertView;
            }

            @Override
            public int getChildrenCount(int groupPosition) {
if(this._listDataChild.get(this._listDataHeader.get(groupPosition)).size()==0)
                {

                    Toast.makeText(this._context, "No Data Found", Toast.LENGTH_SHORT).show();
                    return 0;
                }
                else {
                    return 1;
                }
            }

            @Override
            public Object getGroup(int groupPosition) {
                return this._listDataHeader.get(groupPosition);
            }

            @Override
            public int getGroupCount() {
                return this._listDataHeader.size();
            }

            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }

            @Override
            public View getGroupView(final int groupPosition, boolean isExpanded,
                                     View convertView, ViewGroup parent)
            {
                if(_listDataHeader.size()>0)
                {
                    try {
                        CategoryBean bean = (CategoryBean) _listDataHeader.get(groupPosition);
                        ViewHolder _viewHolder;
                        if (convertView == null)
                        {
                            _viewHolder = new ViewHolder();
                            LayoutInflater infalInflater = (LayoutInflater) this._context
                                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            convertView = infalInflater.inflate(R.layout.catlist_row, null);

                            _viewHolder._groupTxt = (TextView) convertView.findViewById(R.id.catTxt);
                            _viewHolder._catIcon = (ImageView) convertView.findViewById(R.id.cat_icon);
                            _viewHolder._catCount = (TextView) convertView.findViewById(R.id.catcount);
                            convertView.setTag(_viewHolder);
                        }
                        else
                        {
                            _viewHolder = (ViewHolder) convertView.getTag();
                        }
                        _viewHolder._groupTxt.setText(bean.getCatName());
                        _viewHolder._catCount.setText(String.valueOf(bean.getChildArrayList().size()));

                        if(isExpanded)
                        {
                            if (groupPosition == 0)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.food_and_drinkblue);
                            }
                            if (groupPosition == 1)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.computersblue);
                            }
                            if (groupPosition == 2)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.computersblue);
                            }
                            if (groupPosition == 3)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.tools_and_homeblue);
                            }
                            if (groupPosition == 4)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.babiesblue);
                            }
                            if (groupPosition == 5)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.autoblue);
                            }
                            if (groupPosition == 6)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.clothingsblue);
                            }
                            if (groupPosition == 7)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.hairblue);
                            }
                        }
                        else
                        {
                            if (groupPosition == 0)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.food_and_drinkgrey);
                            }

                            if (groupPosition == 1)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.computersgrey);
                            }
                            if (groupPosition == 2)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.computersgrey);
                            }
                            if (groupPosition == 3)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.tools_and_homegrey);
                            }
                            if (groupPosition == 4)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.babiesgrey);
                            }
                            if (groupPosition == 5)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.autogrey);
                            }
                            if (groupPosition == 6)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.clothingsgrey);
                            }
                            if (groupPosition == 7)
                            {
                                _viewHolder._catIcon.setBackgroundResource(R.drawable.hairgrey);
                            }

                        }
                    }
                    catch (Exception e)
                    {

                    }
                }
                return convertView;
            }

            @Override
            public boolean hasStableIds() {
                return false;
            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return true;
            }
        }

         public class GridAdapter extends BaseAdapter
            {
                Activity activity;
                private Typeface font;
                public ArrayList<SubCatBean> data = new ArrayList<>();

                public GridAdapter(Activity activity,ArrayList<SubCatBean> data)
                {
                    this.activity = activity;
                    this.data = data;
                }

                public int getCount() {
                    return data.size();
                }

                @Override
                public Object getItem(int i) {
                    return data.get(i);
                }

                @Override
                public long getItemId(int i) {
                    return i;
                }

                public class ViewHolder
                {
                    public TextView _childBtn = null;
                }

                @Override
                public View getView(final int i,View convertView, ViewGroup viewGroup)
                {        // LayoutInflator to call external grid_item.xml file
                    final SubCatBean bean = (SubCatBean)data.get(i);
                    try {
                        ViewHolder  view_holder;
                        if (convertView == null)
                        {
                            view_holder = new ViewHolder();
                            LayoutInflater inflater = (LayoutInflater) activity
                                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            convertView = inflater.inflate(R.layout.sub_cat_row, null);
                            // set value into text view
                            view_holder._childBtn = (TextView) convertView.findViewById(R.id.childTxt);

                            convertView.setTag(view_holder);
                        }
                        else
                        {
                            view_holder = (ViewHolder) convertView.getTag();
                        }
                        //------------------------//----------------------------------//
                        if (data.get(i).getSubCatStatus().equalsIgnoreCase("0"))
                        {
                            view_holder._childBtn.setBackgroundResource(R.drawable.grey);
                            view_holder._childBtn.setTextColor(getResources().getColor(R.color.grey));
                            bean.setSubCatValue(0);
                        }
                        else
                        {
                            view_holder._childBtn.setBackgroundResource(R.drawable.blue);
                            view_holder._childBtn.setTextColor(getResources().getColor(R.color.blue));
                            bean.setSubCatValue(1);
                        }
                        view_holder._childBtn.setText(bean.getSubCatName());

                        view_holder._childBtn.setOnClickListener(new View.OnClickListener()
                        {
                            @Override
                            public void onClick(View v)
                            {
                                System.out.println("child position" + i);
                                if(SharedPref.getSharedPrefData(getActivity(),SharedPref.PREFRENCE_KEY_LOGIN).length()==0)
                                {
                                    if (data.get(i).getSubCatValue() == 0)
                                    {
                                        bean.setSubCatValue(1);
                                        bean.setSubCatStatus("1");
                                        addData.add(bean.getSubCatID());
                                        System.out.println("add value" + bean.getSubCatID());
                                    }
                                    else
                                    {
                                        bean.setSubCatValue(0);
                                        bean.setSubCatStatus("0");
                                        for (int j = 0; j < addData.size(); j++)
                                        {
                                            if (addData.get(j).equals(bean.getSubCatID()))
                                            {
                                                addData.remove(j);
                                                System.out.println("add value remove"+bean.getSubCatID());
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    if (data.get(i).getSubCatValue() == 0)
                                    {
                                        bean.setSubCatValue(1);
                                        bean.setSubCatStatus("1");
                                        main_valueAfter = String.valueOf(bean.getSubCatID());
                                        true_valueAfter = "1";
                                    }
                                    else
                                    {
                                        bean.setSubCatValue(0);
                                        bean.setSubCatStatus("0");
                                        main_valueAfter = String.valueOf(bean.getSubCatID());
                                        true_valueAfter = "0";
                                    }
                                    callSetCategory();
                                }

                                notifyDataSetChanged();
                            }
                        });

                    }catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                    return convertView;
                }

            }
cat_child_row.xml

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

    <GridView
        android:id="@+id/sub_cat_grid"
        android:layout_width="match_parent"
        android:numColumns="3"
        android:listSelector="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:layout_height="wrap_content"
        >
    </GridView>
</RelativeLayout>

sub_cat_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/childTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="10sp"
        android:background="@drawable/grey"/>

</RelativeLayout>


它不是
NPE
。设置错误的布局参数
。是否已动态设置布局参数?@kishorejethava是。。我已编辑我的代码。请更改
if(groupPosition==x)
进入
开关的系列
语句显示您的
cat_child_row.xml
文件。发布您的xml布局它可以帮助我们确定异常的原因+引发类强制转换异常以指示代码试图将对象强制转换为其不是实例的子类。因此,xml可能有助于我们更好地理解这个问题,它不是
NPE
。设置错误的布局参数
。是否已动态设置布局参数?@kishorejethava是。。我已编辑我的代码。请更改
if(groupPosition==x)
进入
开关的系列
语句显示您的
cat_child_row.xml
文件。发布您的xml布局它可以帮助我们确定异常的原因+引发类强制转换异常以指示代码试图将对象强制转换为其不是实例的子类。因此,xml可以帮助我们更好地理解这个问题
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/childTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="10sp"
        android:background="@drawable/grey"/>

</RelativeLayout>