我正在尝试创建一个显示菜单项列表的可扩展列表。但是,在执行时,单击新活动的按钮会导致致命异常。
我的源代码如下所示:
public class orders extends ExpandableListActivity{
//public String NAME="DishName";
//ExpandableListView explistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.orders);
SimpleExpandableListAdapter expListAdapter=
new SimpleExpandableListAdapter(
this,
createGroupList(),
R.layout.group_row,
new String[] { "Group Item" },
new int[] { R.id.row_name },
createChildList(),
R.layout.child_row,
new String[] {"Sub Item"},
new int[] { R.id.grp_child}
);
setListAdapter(expListAdapter);
}
public List createGroupList()
{
ArrayList result = new ArrayList();
HashMap m=new HashMap();
m.put("Group Item","PIZZA's");
result.add( m );
/* m.put("Group Item","PASTRIES");
result.add( m );
m.put("Group Item","CHATS");
result.add( m );
m.put("Group Item","MEALS");
result.add( m );*/
return (List)result;
}
public List createChildList()
{
ArrayList result = new ArrayList();
ArrayList secList = new ArrayList();
HashMap child = new HashMap();
child.put( "Sub Item", "Brick oven pizza");
secList.add( child );
child.put( "Sub Item", "Chicago style");
secList.add( child );
child.put( "Sub Item", "French break pizza");
secList.add( child );
child.put( "Sub Item", "Italian pizza");
secList.add( child );
child.put( "Sub Item", "New York style pizza");
secList.add( child );
result.add( secList );
child.put( "Sub Item", "Black Forest");
secList.add( child );
child.put( "Sub Item", "Chocolate Moose");
secList.add( child );
result.add( secList );
child.put( "Sub Item", "Bhel Puri");
secList.add( child );
child.put( "Sub Item", "Paani Puri");
secList.add( child );
child.put( "Sub Item", "Masala Puri");
secList.add( child );
child.put( "Sub Item", "Samosa Masala");
secList.add( child );
result.add( secList );
child.put( "Sub Item", "North Indian Meals");
secList.add( child );
child.put( "Sub Item", "South Indian Meals");
secList.add( child );
result.add( secList );
return result;
}
public void onContentChanged () {
//System.out.println("onContentChanged");
Log.d("MYAPP", "onContentChanged....");
super.onContentChanged();
}
/* This function is called on each child click */
public boolean onChildClick( ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
//System.out.println("Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition);
Log.d("MYAPP","Inside onChildClick at groupPosition = " + groupPosition +" Child clicked at position " + childPosition);
return true;
}
/* This function is called on expansion of the group */
public void onGroupExpand (int groupPosition) {
try{
//System.out.println("Group exapanding Listener => groupPosition = " + groupPosition);
Log.d("MYAPP","Group exapanding Listener => groupPosition = " + groupPosition);
}catch(Exception e){
//System.out.println(" groupPosition Errrr +++ " + e.getMessage());
Log.d("MYAPP"," groupPosition Errrr +++ " + e.getMessage());
}
}
}
以下是异常的详细信息:
04-09 12:22:02.500: E/AndroidRuntime(22571): FATAL EXCEPTION: main
04-09 12:22:02.500: E/AndroidRuntime(22571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.takemyorder/com.takemyorder.orders}: java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'
04-09 12:22:02.500: E/AndroidRuntime(22571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
04-09 12:22:02.500: E/AndroidRuntime(22571): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
04-09 12:22:02.500: E/AndroidRuntime(22571): at android.app.ActivityThread.access$600(ActivityThread.java:134)
04-09 12:22:02.500: E/AndroidRuntime(22571): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
04-09 12:22:02.500: E/AndroidRuntime(22571): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 12:22:02.500: E/AndroidRuntime(22571): at android.os.Looper.loop(Looper.java:154)
04-09 12:22:02.500: E/AndroidRuntime(22571): at android.app.ActivityThread.main(ActivityThread.java:4624)
04-09 12:22:02.500: E/AndroidRuntime(22571): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 12:22:02.500: E/AndroidRuntime(22571): at java.lang.reflect.Method.invoke(Method.java:511)
04-09 12:22:02.500: E/AndroidRuntime(22571): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
04-09 12:22:02.500: E/AndroidRuntime(22571): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
04-09 12:22:02.500: E/AndroidRuntime(22571): at dalvik.system.NativeStart.main(Native Method)
04-09 12:22:02.500: E/AndroidRuntime(22571): Caused by: java.lang.RuntimeException: Your content must have a ExpandableListView whose id attribute is 'android.R.id.list'
04-09 12:22:02.500: E/AndroidRuntime(22571): at android.app.ExpandableListActivity.onContentChanged(ExpandableListActivity.java:222)
04-09 12:22:02.500: E/AndroidRuntime(22571): at com.takemyorder.orders.onContentChanged(orders.java:110)
04-09 12:22:02.500: E/AndroidRuntime(22571): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:257)
04-09 12:22:02.500: E/AndroidRuntime(22571): at android.app.Activity.setContentView(Activity.java:1837)
04-09 12:22:02.500: E/AndroidRuntime(22571): at com.takemyorder.orders.onCreate(orders.java:26)
04-09 12:22:02.500: E/AndroidRuntime(22571): at android.app.Activity.performCreate(Activity.java:4479)
04-09 12:22:02.500: E/AndroidRuntime(22571): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
04-09 12:22:02.500: E/AndroidRuntime(22571): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
04-09 12:22:02.500: E/AndroidRuntime(22571): ... 11 more
任何人都可以建议在上面的代码中进行更改。提前致谢。