创建可扩展列表时的Android运行时异常

时间:2013-04-09 07:02:29

标签: android android-listview

我正在尝试创建一个展开菜单项列表的可扩展列表。但是,执行时,单击“开启”按钮会导致新的活动导致“致命异常”。

我的源代码如下所示:

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

任何人都可以建议在上面的代码中执行更改。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的展开式列表必须具有ID android:id="@+id/android:list",才能在orders.xml中为ExpandableList设置此属性。请检查此ExpandableListActivity

示例:

  <ExpandableListView android:id="@id/android:list"
               android:layout_width="match_parent" 
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>