微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – java.lang.UnsupportedOperationException:AdapterView不支持addView(View,LayoutParams)

我正在使用在网络上找到的Expandable ListView示例

活动:

public class ExpandableListViewActivity extends ExpandableListActivity {
    /**
     * strings for group elements
     */
    static final String arrGroupelements[] = { "India","Australia","England","South Africa" };

    /**
     * strings for child elements
     */
    static final String arrChildelements[][] = {
            { "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 expList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        expList = getExpandableListView();
        metrics = new displayMetrics();
        getwindowManager().getDefaultdisplay().getMetrics(metrics);
        width = metrics.widthPixels;
        // this code for adjusting the group indicator into right side of the
        // view
        expList.setIndicatorBounds(width - GetDipsFromPixel(50),width
                - GetDipsFromPixel(10));
        expList.setAdapter(new ExpAdapter(this));

        expList.setonGroupExpandListener(new OnGroupExpandListener() {
            public void onGroupExpand(int groupPosition) {
                Log.e("onGroupExpand","OK");
            }
        });

        expList.setonGroupCollapseListener(new OnGroupCollapseListener() {
            public void onGroupCollapse(int groupPosition) {
                Log.e("onGroupCollapse","OK");
            }
        });

        expList.setonChildClickListener(new OnChildClickListener() {
            public boolean onChildClick(ExpandableListView parent,View v,int groupPosition,int childPosition,long id) {
                Log.e("OnChildClickListener","OK");
                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);
    }
}

适配器:

public class ExpAdapter extends Baseexpandablelistadapter {

    private Context myContext;

    public ExpAdapter(Context context) {
        myContext = context;
    }

    public Object getChild(int groupPosition,int childPosition) {
        return null;
    }

    public long getChildId(int groupPosition,int childPosition) {
        return 0;
    }

    public View getChildView(int groupPosition,boolean isLastChild,View convertView,ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) myContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.child_row,null);
        }

        TextView tvPlayerName = (TextView) convertView
                .findViewById(R.id.tvPlayerName);
        tvPlayerName
                .setText(ExpandableListViewActivity.arrChildelements[groupPosition][childPosition]);

        return convertView;
    }

    public int getChildrenCount(int groupPosition) {
        return ExpandableListViewActivity.arrChildelements[groupPosition].length;
    }

    public Object getGroup(int groupPosition) {
        return null;
    }

    public int getGroupCount() {
        return ExpandableListViewActivity.arrGroupelements.length;
    }

    public long getGroupId(int groupPosition) {
        return 0;
    }

    public View getGroupView(int groupPosition,boolean isExpanded,ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) myContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.group_row,null);
        }

        TextView tvGroupName = (TextView) convertView
                .findViewById(R.id.tvGroupName);
        tvGroupName
                .setText(ExpandableListViewActivity.arrGroupelements[groupPosition]);

        return convertView;
    }

    public boolean hasstableIds() {
        return false;
    }

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

main.xml中:

<?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/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:groupIndicator="@drawable/group_indicator" >

        <TextView
            android:id="@+id/android:empty"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="No Items" >
        </TextView>
    </ExpandableListView>

</LinearLayout>

我试过这个解决方案,但没有工作:(

Android: Custom ListAdapter extending BaseAdapter crashes on application launch

它被告知要添加第三个参数“假”,到inflater.inflate(R.layout.group_row,null,false);

解决方法

将TextView移动到ExpandableListView元素之外:
<?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/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:groupIndicator="@drawable/group_indicator" >
    </ExpandableListView>
    <TextView
            android:id="@+id/android:empty"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="No Items" >
    </TextView>
</LinearLayout>

AdapterView的子类(如ExpandableListView)不能在xml布局中拥有子项(就像您在布局中所做的那样).

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Android 通过adb shell命令查看内存,CPU,启动时间,电量等信息 by:授客 QQ:1033553122 1、 查看内存信息 1)查看所有内存信息 命令: dumpsys meminfo 例: C:\Users\laiyu&gt;adb shell shell@android:/ $
Monkey Android app稳定性测试工具之Monkey使用教程 by:授客 QQ:1033553122 由于篇幅问题,仅提供百度网盘下载链接: Android app稳定性测试工具之Monkey使用教程.pdf
Android 常见adb命令 by:授客 QQ:1033553122 1、 查看所有已链接的设备 命令: adb devices 例: C:\Users\laiyu&gt;adb devices List of devices attached 5d3b5aac device 设备命令 2、 复制
这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方法有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android岛屿数量算...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Andro...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android数据压缩的方法是什么”文章能帮助大家解决疑惑...
这篇“Android怎么使用Intent传大数据”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅...