android - ClassCastException android.view.viewgroup.$layoutParams

标签 android android-layout android-widget

 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 中组 View 的代码 fragment ,它给出了错误:

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

最佳答案

确保导入正确的 LayoutParams

  • android.widget.LinearLayout.LayoutParams 用于 LinearLayout
  • android.view.ViewGroup.LayoutParams 用于 TextView

关于android - ClassCastException android.view.viewgroup.$layoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7547957/

相关文章:

android - 如何在android中显示带有月份和年份的标题?

java - 使用layout_weight而不被拉伸(stretch)

android - 通过 ID token 从服务器检索 Google 用户数据

android - 自定义搜索栏被切边

java - 将进度条设置为在文本完成后增加

java - 如何使用 OnTabChangeListener?

android - 如何定期更新小部件,比如每 5 秒更新一次

android - 如何更改 RemoteViews 中的文本大小

android - 将 Android Gradle 插件更新到 3.2.0 或更高版本/生成 Flutter 签名的 APK

java - 测试静态方法类必须更改为单例以进行 JUnit 测试