2013-11-15 35 views
0

我已經刪除了所有的意見,然後添加一個視圖仍然是得到這個錯誤。指定的孩子已經有一個家長,你必須調用removeview

btn2.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 


       if(isHierarchical){ 

        IndexTableActivity.this.setContentView(R.layout.activity_main); 
        final ExpandableListView expListView = (ExpandableListView) findViewById(R.id.laptop_list); 

        booksLV = (ListView)findViewById(R.id.booksLV); 
        System.out.println("THE NUMBERRRRRRRRRRRRRRRRRRRR "+expListView); 
        btn2.setText("L"); 
        isHierarchical=false; 
         createGroupList(); 
         createCollection(); 


         final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
           IndexTableActivity.this, groupList, laptopCollection); 

         IndexTableActivity.this. setContentView(R.layout.main); 

         LinearLayout listView = (LinearLayout) findViewById(R.id.listView); 
         ViewGroup parentViewGroup = (ViewGroup) listView.getParent(); 
         if (parentViewGroup != null) { 
          parentViewGroup.removeAllViewsInLayout();; 
          parentViewGroup.removeAllViews(); 

         } 
         listView.removeAllViews(); 

         listView.addView(expListView);// am getting error here 
         expListView.setAdapter(expListAdapter); 
       }else{ 
        btn2.setText("H"); 

        isHierarchical=true; 
       } 
      } 
     }); 

請幫我找出爲什麼得到這個錯誤?以及如何解決它。

錯誤日誌

enter image description here 謝謝

+0

你expListView早已爲人父母,因爲它是在activity_main我假設......所以,問題與expListView不使用ListView – Alex

+0

@Alex它存在於activity_main但在那之後我已經改變了內容視圖爲「主」IndexTableActivity.this。的setContentView(R.layout.main);然後我試圖添加expListView到主(實際上expListView屬於activity_main) – Amith

+0

@Amith請發佈一些更多的代碼與logcat錯誤。 – GrIsHu

回答

0

從你的代碼有點複雜,瞭解你想要達到的目的。因爲首先你要添加布局,然後直接刪除它,然後再將視圖添加到已經刪除的佈局中。真奇怪。

刪除listView.removeAllViews();因爲你已經刪除的視圖,然後您要添加另一種觀點進去之後。這是錯誤的方式。

刪除下面的代碼:

  if (parentViewGroup != null) { 
         parentViewGroup.removeAllViewsInLayout();; 
         parentViewGroup.removeAllViews(); 

        } 
     listView.removeAllViews(); 

只需添加視圖到佈局第一。

 listView.addView(expListView);// am getting error here 
    expListView.setAdapter(expListAdapter); 
+0

我有一個頁面,其中包括一個搜索字段的按鈕和列表。在按鈕上單擊我必須將普通列表視圖更改爲可擴展列表視圖這正是我正在嘗試執行的操作 – Amith

+0

@GrlsHu因此,我將刪除現有列表視圖並添加新列表視圖。 – Amith

相關問題