Inflate可扩展列表视图中的错误

时间:2014-01-27 06:49:33

标签: android android-layout

我尝试对膨胀列表视图进行膨胀,但它显示错误就像这样......

01-27 06:37:34.619: E/AndroidRuntime(7522): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.info.wishlist/com.info.wishlistadapter.Expand_list}: java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'
01-27 06:37:34.619: E/AndroidRuntime(7522):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at android.os.Looper.loop(Looper.java:137)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at java.lang.reflect.Method.invokeNative(Native Method)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at java.lang.reflect.Method.invoke(Method.java:511)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at dalvik.system.NativeStart.main(Native Method)
01-27 06:37:34.619: E/AndroidRuntime(7522): Caused by: java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'
01-27 06:37:34.619: E/AndroidRuntime(7522):     at android.app.ExpandableListActivity.onContentChanged(ExpandableListActivity.java:222)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:259)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at android.app.Activity.setContentView(Activity.java:1867)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at com.info.wishlistadapter.Expand_list.onCreate(Expand_list.java:41)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at android.app.Activity.performCreate(Activity.java:5008)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-27 06:37:34.619: E/AndroidRuntime(7522):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-27 06:37:34.619: E/AndroidRuntime(7522):     ... 11 more

这个班级.....

public class Expand_list extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
WishListHelper dbhelper;
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.expand);
    dbhelper = new WishListHelper(this);
    String URL = "content://com.info.provider.Wish_List_Name/WISH_LIST1";
    Uri wishlist = Uri.parse(URL);
    ContentResolver cus = getContentResolver();
    Cursor c = cus.query(wishlist, null, null, null, null);
    mAdapter = new MyExpandableListAdapter(this, c);
    setListAdapter(mAdapter);

}

class MyExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;
    Cursor data;
    int add = 0;
    String[] array;

    ArrayList<String> al = new ArrayList<String>();

    public MyExpandableListAdapter(Context context, Cursor c) {
        super();
        this.mContext = context;
        this.data = c;

        array = new String[c.getCount() + 2];
        int i = 0;
        if (data.moveToFirst()) {

            do {

                String array1 = data.getString(data
                        .getColumnIndex(Wish_list_Table.COL_CATEGORY));

                array[i] = array1;
                Log.i("array", "" + array[i]);
                Log.i("arrayl", "" + i);
                i++;
            } while (data.moveToNext());

        }
        data.close();
        grouparray();
        // TODO Auto-generated constructor stub
    }

    private void grouparray() {

        int n = data.getCount();

        for (int i = 0; i < n; i++) {
            if (!al.contains(array[i])) {
                al.add(add++, array[i]);

            }
        }
        for (int j = 0; j < add - 1; j++) {
            Log.i("al", "" + al.get(j));

        }

    }

    public TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

        TextView textView = new TextView(Expand_list.this);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        textView.setPadding(36, 0, 0, 0);
        return textView;
    }

    @Override
    public Object getChild(int arg0, int arg1) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getChildId(int arg0, int arg1) {
        // 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
        TextView name = (TextView) findViewById(R.id.textView1);
        TextView desc = (TextView) findViewById(R.id.edesc);
        TextView date = (TextView) findViewById(R.id.edate);
        TextView time = (TextView) findViewById(R.id.etime);
        CheckBox check = (CheckBox) findViewById(R.id.checkBox1);
        Button ebtn = (Button) findViewById(R.id.button1);

        db = dbhelper.getReadableDatabase();

        String selectSQL = "Select * from " + Wish_list_Table.TABLE_NAME
                + " where " + Wish_list_Table.COL_CATEGORY + " = ?";

        String[] add = { "" + al.get(groupPosition) };

        TextView textView = getGenericView();
        Cursor selectdata = db.rawQuery(selectSQL, add);

        if (selectdata.moveToFirst()) {
            do {
                name.setText(selectdata.getString(selectdata
                        .getColumnIndex(Wish_list_Table.COL_NAME)));
                desc.setText(selectdata.getString(selectdata
                        .getColumnIndex(Wish_list_Table.COL_DATE)));
                date.setText(selectdata.getString(selectdata
                        .getColumnIndex(Wish_list_Table.COL_DATE)));
                time.setText(selectdata.getString(selectdata
                        .getColumnIndex(Wish_list_Table.COL_TIME)));
                ebtn.setText(selectdata.getString(selectdata
                        .getColumnIndex(Wish_list_Table.COL_CATEGORY)));
            } while (selectdata.moveToNext());

        }

        Log.i("str", "" + selectdata.getCount());
        return textView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        Log.i("next", "count");
        db = dbhelper.getReadableDatabase();

        String selectSQL = "Select * from " + Wish_list_Table.TABLE_NAME
                + " where " + Wish_list_Table.COL_CATEGORY + " = ?";

        String[] add = { "" + al.get(groupPosition) };

        Cursor selectdata = db.rawQuery(selectSQL, add);
        return selectdata.getCount();
    }

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

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub

        return al.size();
    }

    @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) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        View custom = inflater.inflate(R.layout.expanditem, null);
        TextView textView = (TextView) findViewById(R.id.explist);
        textView.setText("" + al.get(groupPosition));
        return custom;
    }

    @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 false;
    }

}

}

这是xml文件... 1)expand.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ExpandableListView
    android:id="@+id/expandableListView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ExpandableListView>

2)expandlist.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/explist"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center|top"
    android:text="TextView" />

3)expanditem.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


<TextView
    android:id="@+id/edesc"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="false"
    android:layout_below="@id/textView1"
    android:lines="4"
    android:paddingTop="4dp"
    android:text="Description" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/edesc"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Button" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="200dp"
    android:layout_height="30dp"
    android:layout_alignBaseline="@+id/checkBox1"
    android:layout_alignBottom="@+id/checkBox1"
    android:layout_toLeftOf="@+id/button1"
    android:gravity="center"
    android:text="Name" />

<TextView
    android:id="@+id/edate"
    android:layout_width="180dp"
    android:layout_height="20dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/edesc"
    android:text="Date" />

<TextView
    android:id="@+id/etime"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/edesc"
    android:layout_toRightOf="@+id/edate"
    android:text="Time" />

1 个答案:

答案 0 :(得分:1)

Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'

在您的布局中,expand.xml将@+id/expandableListView1更改为@android:id/list 在您的代码中,将R.id.expandableListView1替换为android.R.id.list