无法将Android.widget.linearlayout强制转换为Android.widget.Tablelayout
我试图在代码中的同一行中添加两个LinearLayout,但显示错误:无法将Android.widget.linearlayout强制转换为Android.widget.Tablelayout,android,android-layout,runtime-error,android-linearlayout,android-tablelayout,Android,Android Layout,Runtime Error,Android Linearlayout,Android Tablelayout,我试图在代码中的同一行中添加两个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 contr
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声明从linearlayout更改为tablelayout。
抱歉请问xml文件在哪里?这可能是LinerLayout TableLayout findViewByIdR.id.main;顺便说一句,您不能多次添加同一视图-tr.addViewcontrow;tr.addViewcontrow@Naveen Tamrakar感谢您的回复。我犯了一个错误。谢谢