简体   繁体   中英

Android Runtime Exception while creating expandable list

I'm trying to create an expandable list which shows a list of menu items. However while execution, On button click to new activity leads to FATAL EXCEPTION.

My Source code is shown below:

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());
        }
    }

}

Below are the details of the exception:

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

Can anybody suggest changes to be performed in the above code. Thanks in advance.

Your Expandable List must have the ID android:id="@+id/android:list" set this attribute in your orders.xml for ExpandableList . Check this ExpandableListActivity

Example:

  <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"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM