2013-04-09 68 views
0

我試圖創建一個展開菜單項列表的可展開列表。然而,在執行時,按鈕點擊新的活動導致致命的例外。創建可擴展列表時Android運行時異常

我的源代碼如下所示:

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 

任何人都可以表明,在上面的代碼進行修改。提前致謝。

回答

0

對於ExpandableList,您的Expandable列表必須在您的orders.xml的ID android:id="@+id/android:list"中設置此屬性。勾選此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"/> 
+0

你是最好的。它的工作..感謝了很多 – Zax 2013-04-09 08:05:37