ExpandableListView -UnsupportedOperationException:addView(查看,的LayoutParams)不支持适配器视图 [英] ExpandableListView -UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

查看:189
本文介绍了ExpandableListView -UnsupportedOperationException:addView(查看,的LayoutParams)不支持适配器视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施的Andr​​oid扩展列表视图,我收到上述标题错误。请帮我。

主要业务是 -

 包com.expand;

进口android.app.Activity;
进口android.os.Bundle;
进口android.util.DisplayMetrics;
进口android.util.Log;
进口android.view.View;
进口android.widget.ExpandableListView;
进口android.widget.Toast;


公共类MyExpandableListViewActivity延伸活动{
    / **第一次创建活动时调用。 * /



    静态最后弦乐groupElements [] = {
           印度,
           澳大利亚,
           英国,
           南非
         };

    静态最终的String [] [] childElements =
    {
           {
          邓德,
          雷纳
          多尼船,
          Yuvi
           },
           {
          庞廷
          亚当吉尔克里斯特
          迈克尔·克拉克
           },
           {
          安德鲁·施特劳斯
          凯文·彼得森,
          纳赛尔·侯赛因
           },
           {
          格雷姆·史密斯,
          AB维里埃
          雅克的Kallis
           }
            };



    DisplayMetrics指标;
    INT宽度;
    ExpandableListView expandList;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        expandList =(ExpandableListView)findViewById(R.id.expandList1);
        指标=新DisplayMetrics();

        。getWindowManager()getDefaultDisplay()getMetrics(度量)。
        宽度= metrics.widthPixels;

        // ExpAdapter适配器=新ExpAdapter(MyExpandableListViewActivity.this,groupElements,childElements);

        expandList.setAdapter(新ExpAdapter(MyExpandableListViewActivity.this,groupElements,childElements));
        expandList.setIndicatorBounds(宽 -  GetDipsFromPixel(50),宽度 -  GetDipsFromPixel(10));


        expandList.setOnGroupExpandListener(新ExpandableListView.OnGroupExpandListener(){

            @覆盖
            公共无效onGroupExpand(INT groupPosition){
                // TODO自动生成方法存根

                 Log.e(onGroupExpand,OK);
            }
        });

        expandList.setOnGroupCollapseListener(新ExpandableListView.OnGroupCollapseListener(){

            @覆盖
            公共无效onGroupCollapse(INT groupPosition){
                // TODO自动生成方法存根

                Log.e(onGroupCollapse,OK);

            }
        });

        expandList.setOnChildClickListener(新ExpandableListView.OnChildClickListener(){



            @覆盖
            公共布尔onChildClick(ExpandableListView父,视图V,
                    INT groupPosition,诠释childPosition,长ID){

                //获取所选择的项目
                //的String =(字符串)expandList.getItemAtPosition((INT)ID);

                Toast.makeText(MyExpandableListViewActivity.this,您已选择:Toast.LENGTH_SHORT).show();

                返回false;
            }
        });

    }



    公众诠释GetDipsFromPixel(浮点像素)
    {
        //获取屏幕的密度天平
        最终浮动规模= getResources()getDisplayMetrics()密度。
        //将DPS像素,基于密度规模
        返程(INT)(像素*规模+ 0.5F);
    }


}
 

ExpAdapter类是 - 我已经实现在其他类适配器,并在山的主要活动称之为

 包com.expand;

进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseExpandableListAdapter;
进口android.widget.TextView;



    公共类ExpAdapter扩展BaseExpandableListAdapter {

        公共语境myContext;
        公众的String [] [] childElements;
        TextView的childValues​​;
        公众的String [] groupElements;


        公共ExpAdapter(上下文的背景下,的String []组的String [] []孩子的)
        {

            this.myContext =背景;
            this.groupElements =组;
            this.childElements =孩子的;

        }



        @覆盖
        公共对象getChild(INT groupPosition,诠释childPosition){
            // TODO自动生成方法存根
            返回childElements [groupPosition] [childPosition]
        }

        @覆盖
        众长getChildId(INT groupPosition,诠释childPosition){
            // TODO自动生成方法存根

            返回0;
        }

        @覆盖
        公共查看getChildView(INT groupPosition,INT childPosition,
                布尔isLastChild,查看convertView,ViewGroup中父){
            // TODO自动生成方法存根

            如果(convertView == NULL){

                LayoutInflater充气=(LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflator.inflate(R.layout.child_rows,父);

            }
            childValues​​ =(TextView中)convertView.findViewById(R.id.rowValues​​);
            childValues​​.setText(childElements [groupPosition] [childPosition]);

            返回convertView;
        }

        @覆盖
        公众诠释getChildrenCount(INT groupPosition){
            // TODO自动生成方法存根
            返回groupElements [groupPosition] .length();
        }

        @覆盖
        公共对象getGroup(INT groupPosition){
            // TODO自动生成方法存根
            返回groupElements [groupPosition]
        }

        @覆盖
        公众诠释getGroupCount(){
            // TODO自动生成方法存根
            返回groupElements.length;
        }

        @覆盖
        众长getGroupId(INT groupPosition){
            // TODO自动生成方法存根
            返回0;
        }

        @覆盖
        公共查看getGroupView(INT groupPosition,布尔isExpanded,
                查看convertView,ViewGroup中父){
            // TODO自动生成方法存根

            如果(convertView == NULL){
                LayoutInflater充气=(LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflator.inflate(R.layout.group_rows,NULL);
            }
            TextView的组=(TextView中)convertView.findViewById(R.id.groupId);
            group.setText(groupElements [groupPosition]);

            返回convertView;
        }

        @覆盖
        公共布尔hasStableIds(){
            // TODO自动生成方法存根
            返回false;
        }

        @覆盖
        公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
            // TODO自动生成方法存根
            返回true;
        }




    }
 

main.xml中 -

这是受主的活性显示在第一所述XNL

 < XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:方向=垂直>



        < ExpandableListView
            机器人:ID =@ + ID / expandList1
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            >


                 <的TextView
                机器人:ID =@ + ID /安卓:空
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                >
            < / TextView的>


        < / ExpandableListView>


    < / LinearLayout中>
 

group_row.xml

这是XML的元素组

 < XML版本=1.0编码=UTF-8&GT?;
    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID / gropu_name
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =40dp
        机器人:方向=垂直>


        <的TextView
            机器人:ID =@ + ID /的groupId
            机器人:layout_height =40dp
            机器人:layout_width =WRAP_CONTENT
            机器人:以下属性来=30dp
            机器人:重力=center_vertical
            机器人:TEXTSIZE =16DP
            机器人:TEXTSTYLE =黑体
            />

    < / LinearLayout中>
 

child_row.xml 这是XML的子元素

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =40dp
    机器人:方向=横向>

    <的TextView
        机器人:ID =@ + ID / rowValues​​
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =30dp
        机器人:重力=center_vertical
        机器人:以下属性来=50dp
        机器人:TEXTSIZE =12dp/>


< / LinearLayout中>
 

解决方案

好像适配器视图不允许添加新的观点, 我遇到过同样的问题。

替换以下行解决这个问题。

  convertView = inflator.inflate(R.layout.child_rows,父);
 

  convertView = inflator.inflate(R.layout.child_rows,NULL);
 

I am implementing Expandable List view in android and i am getting the above titled error. Please help me.

Main activity is -

package com.expand;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;


public class MyExpandableListViewActivity extends Activity {
    /** Called when the activity is first created. */



    static final String groupElements[]= {
           "India",
           "Australia",
           "England",
           "South Africa"
         };

    static final String[][] childElements=
    {
           {
          "Sachin Tendulkar",
          "Raina",
          "Dhoni",
          "Yuvi"
           },
           {
          "Ponting",
          "Adam Gilchrist",
          "Michael Clarke"
           },
           {
          "Andrew Strauss",
          "kevin Peterson",
          "Nasser Hussain"
           },
           {
          "Graeme Smith",
          "AB de villiers",
          "Jacques Kallis"
           }
            };



    DisplayMetrics metrics;
    int width;
    ExpandableListView expandList;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        expandList = (ExpandableListView)findViewById(R.id.expandList1);
        metrics = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;

        //ExpAdapter adapter = new ExpAdapter(MyExpandableListViewActivity.this, groupElements, childElements);

        expandList.setAdapter(new ExpAdapter(MyExpandableListViewActivity.this, groupElements, childElements));
        expandList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));


        expandList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                // TODO Auto-generated method stub

                 Log.e("onGroupExpand", "OK");
            }
        });

        expandList.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                // TODO Auto-generated method stub

                Log.e("onGroupCollapse", "OK");

            }
        });

        expandList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {



            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {

                //getting the item that is selected
                //String s = (String) expandList.getItemAtPosition((int) id);

                Toast.makeText(MyExpandableListViewActivity.this, "You have selected :"  , Toast.LENGTH_SHORT).show();

                return false;
            }
        });

    }



    public int GetDipsFromPixel(float pixels)
    {
        // Get the screen's density scale
        final float scale = getResources().getDisplayMetrics().density;
        // Convert the dps to pixels, based on density scale
        return (int) (pixels * scale + 0.5f);
    }


}

ExpAdapter class is - I have implemented the adapter in other class and have called it in mt main activity

package com.expand;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;



    public class ExpAdapter extends BaseExpandableListAdapter {

        public Context myContext;
        public String[][] childElements;
        TextView childValues;
        public String[] groupElements;


        public ExpAdapter(Context context, String[] group, String[][] childs)
        {

            this.myContext=context;
            this.groupElements = group;
            this.childElements = childs;

        }



        @Override
        public Object getChild(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return childElements[groupPosition][childPosition];
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub

            return 0;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            if(convertView==null){

                LayoutInflater inflator = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView  = inflator.inflate(R.layout.child_rows, parent);

            }
            childValues = (TextView)convertView.findViewById(R.id.rowValues);
            childValues.setText(childElements[groupPosition][childPosition]);

            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            // TODO Auto-generated method stub
            return groupElements[groupPosition].length();
        }

        @Override
        public Object getGroup(int groupPosition) {
            // TODO Auto-generated method stub
            return groupElements[groupPosition];
        }

        @Override
        public int getGroupCount() {
            // TODO Auto-generated method stub
            return groupElements.length;
        }

        @Override
        public long getGroupId(int groupPosition) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            if(convertView==null){
                LayoutInflater inflator = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflator.inflate(R.layout.group_rows, null);
            }
            TextView group = (TextView)convertView.findViewById(R.id.groupId);
            group.setText(groupElements[groupPosition]);

            return convertView;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            // TODO Auto-generated method stub
            return true;
        }




    }

main.xml-

this is the xnl that is displayed at the first by the main activity

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >



        <ExpandableListView 
            android:id="@+id/expandList1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >


                 <TextView 
                android:id="@+id/android:empty"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                >
            </TextView>


        </ExpandableListView>


    </LinearLayout>

group_row.xml

this is the xml for the group elements

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gropu_name"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:orientation="vertical" >


        <TextView 
            android:id="@+id/groupId"
            android:layout_height="40dp"
            android:layout_width="wrap_content"
            android:paddingLeft="30dp"
            android:gravity="center_vertical"
            android:textSize="16dp"
            android:textStyle="bold"
            />

    </LinearLayout>

child_row.xml this is the xml for the child elements

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/rowValues"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:gravity="center_vertical"
        android:paddingLeft="50dp"
        android:textSize="12dp" />


</LinearLayout>

解决方案

Seems like Adapterview does not allow adding new view, I encountered same problem

Solve it by replacing following line

convertView  = inflator.inflate(R.layout.child_rows, parent);

to

convertView  = inflator.inflate(R.layout.child_rows, null);

这篇关于ExpandableListView -UnsupportedOperationException:addView(查看,的LayoutParams)不支持适配器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆