【问题标题】:Android.widget.linearlayout cannot be cast to android.widget.TablelayoutAndroid.widget.linearlayout 无法转换为 android.widget.Tablelayout
【发布时间】:2014-08-27 11:34:01
【问题描述】:

我正在尝试在代码中的同一行中添加两个 LinearLayout,但它显示错误:

java.lang.ClassCastException: Android.widget.linearlayout cannot be cast to android.widget.Tablelayout

我的代码:

TableLayout main = (TableLayout) findViewById(R.id.main);
    TableRow tr = null;
    LinearLayout controw = null;
    for (int i = 0; i < llista.size(); i++) {
        tr = new TableRow(this);
        controw = new LinearLayout(this);

        TextView textView = new TextView(this);
        ImageView img = new ImageView(this);
        Bitmap resizedbitmap = ImagesResources.getInstance()
                .StringResourceToBitmapResized(getResources(),
                        getPackageName(), llista.get(i).getSrcImatge(),
                        200, 200, true);
        img.setImageBitmap(resizedbitmap);
        textView.setText(list.get(i).getNameList());
        controw.addView(textView);
        controw.addView(img);
        tr.addView(controw);
        tr.addView(controw);
        main.addView(tr);
    }

如果我只添加一个 LinearLayout 效果很好,但是当我想添加第二个 LinearLayout 时它会崩溃。 函数 StringResourceToBitmapResized 返回正确的位图。 并且数组列表有 4 个元素。

谢谢

【问题讨论】:

  • xml 文件在哪里,可能是 LinerLayout (TableLayout) findViewById(R.id.main);
  • 顺便说一句。您不能多次添加同一个视图 - tr.addView(controw); tr.addView(controw);
  • @Naveen Tamrakar 感谢您的回复。我犯了一个错误。谢谢。

标签: android android-layout runtime-error android-linearlayout android-tablelayout


【解决方案1】:

对不起, 我犯了一个错误,我试图将表格放在线性布局上。 我将 xml 声明从线性布局更改为表格布局。 对不起

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-07
    • 2020-08-22
    • 2014-01-11
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多