我是android的新手 我想根据这段代码制作一个可扩展的列表:
public class ListexpActivity extends ExpandableListActivity {
ExpandableListView epView;
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ExpandableListAdapter mAdapter;
epView = (ExpandableListView) findViewById(R.id.expandablelistview01);
mAdapter = new MyExpandableListAdapter();
epView.setAdapter(mAdapter);
epView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick( ExpandableListView arg0, View arg1, int groupPosition, long arg3 ) {
if (groupPosition == 5) {
}
return false;
}
});
epView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick( ExpandableListView parent, View v, int groupPosition, int childPosition, long id ) {
if (groupPosition == 0 && childPosition == 0) {
}
return false;
}
});
}
class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for
// groups[i].
private String[] groups = { "Parent1", "Parent2", "Parent3" };
private String[][] children = { { "Child1" }, { "Child2" }, { "Child3" }, { "Child4" }, { "Child5" } };
public Object getChild( int groupPosition, int childPosition ) {
return children[groupPosition][childPosition];
}
public long getChildId( int groupPosition, int childPosition ) {
return childPosition;
}
public int getChildrenCount( int groupPosition ) {
int i = 0;
try {
i = children[groupPosition].length;
} catch (Exception e) {
}
return i;
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(ListexpActivity.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// textView.setTextColor(R.color.marcyred);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
}
public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent ) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
public Object getGroup( int groupPosition ) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId( int groupPosition ) {
return groupPosition;
}
public View getGroupView( int groupPosition, boolean isExpanded, View convertView, ViewGroup parent ) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable( int groupPosition, int childPosition ) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}
和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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ExpandableListView
android:id="@+id/expandablelistview01"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ExpandableListView>
</LinearLayout>
但是在模拟器中实现程序时错误是
05-11 14:38:24.507: E/AndroidRuntime(332): FATAL EXCEPTION: main
05-11 14:38:24.507: E/AndroidRuntime(332): java.lang.RuntimeException: Unable to start activity ComponentInfo{ola.listexp.Main/ola.listexp.Main.ListexpActivity}: java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'
05-11 14:38:24.507: E/AndroidRuntime(332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-11 14:38:24.507: E/AndroidRuntime(332): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-11 14:38:24.507: E/AndroidRuntime(332): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-11 14:38:24.507: E/AndroidRuntime(332): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-11 14:38:24.507: E/AndroidRuntime(332): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 14:38:24.507: E/AndroidRuntime(332): at android.os.Looper.loop(Looper.java:123)
05-11 14:38:24.507: E/AndroidRuntime(332): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-11 14:38:24.507: E/AndroidRuntime(332): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 14:38:24.507: E/AndroidRuntime(332): at java.lang.reflect.Method.invoke(Method.java:521)
05-11 14:38:24.507: E/AndroidRuntime(332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-11 14:38:24.507: E/AndroidRuntime(332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-11 14:38:24.507: E/AndroidRuntime(332): at dalvik.system.NativeStart.main(Native Method)
05-11 14:38:24.507: E/AndroidRuntime(332): Caused by: java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'
05-11 14:38:24.507: E/AndroidRuntime(332): at android.app.ExpandableListActivity.onContentChanged(ExpandableListActivity.java:222)
05-11 14:38:24.507: E/AndroidRuntime(332): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
05-11 14:38:24.507: E/AndroidRuntime(332): at android.app.Activity.setContentView(Activity.java:1647)
05-11 14:38:24.507: E/AndroidRuntime(332): at ola.listexp.Main.ListexpActivity.onCreate(ListexpActivity.java:31)
05-11 14:38:24.507: E/AndroidRuntime(332): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-11 14:38:24.507: E/AndroidRuntime(332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
我是从互联网上知道我应该将@+id/expandablelistview01
更改为@android/idexpandablelistview01
but i have another error in
NOTE: This project contains resource errors, so aapt did not succeed, which can cause rendering failures. Fix resource problems first.
Unable to resolve id "@andriod:id/expandablelistview01" for attribute "id"`
所以帮助我!
答案 0 :(得分:0)
错误确实与您的扩展列表的ID有关。从错误代码中可以看到以下行:
Caused by: java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'
它告诉你的是,在你的XML文件中,与列表关联的ID必须是@android:id / list
那应该清楚了:)