2011-09-25 106 views
4
TextView txt=new TextView(productlist.this); 
      txt.setText(author.get(groupPosition)); 
      TextView txt1=new TextView(productlist.this); 
      txt1.setText(price.get(groupPosition)); 
      txt.setLayoutParams(new LayoutParams(70, LayoutParams.WRAP_CONTENT)); 
      txt1.setLayoutParams(new LayoutParams(70,LayoutParams.WRAP_CONTENT)); 
      LinearLayout ll=new LinearLayout(productlist.this); 
      ll.setOrientation(LinearLayout.HORIZONTAL); 
      ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
      // ll.setGravity(Gravity.CENTER); 
      ll.addView(txt); 
      ll.addView(txt1); 

      return ll; 

嗨,這是在可擴展ListView一組視圖的代碼片段賦予錯誤:

java.lang.ClassCastException android.view.viewgroup.$layoutParam

回答

13

確保導入正確的LayoutParams

  • android.widget.LinearLayout.LayoutParams爲LinearLayout中
  • android.view.ViewGroup.LayoutParams爲TextView的
+0

我有同樣的問題,但在舊的(8版本上運行時,它只是表現出來) 設備。即使我使用'錯誤的'LayoutParams,版本17設備也沒有抱怨。謝謝,Gallal。 –