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

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

问题描述

我使用的净发现扩展的ListView例如

I am using Expandable ListView example found on net

活动:

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, int childPosition,
            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,
            View convertView, 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中:

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>

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

I tried this solution, but didn't work :(

<一个href=\"http://stackoverflow.com/questions/2496698/android-custom-listadapter-extending-baseadapter-crashes-on-application-launch\">Android:自ListAdapter延伸BaseAdapter在应用程序启动崩溃

有它告诉添加第三个参数,假,到inflater.inflate(R.layout.group_row,空,假);

there its told to add a third parameter "false", to the 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>

适配器视图的子类(如 ExpandableListView )不能在一个XML布局儿童(就像你在做您的布局)。

Subclasses of AdapterView(like ExpandableListView) can't have children in a xml layout(like you did in your layout).

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

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