Java ClassCastException:android.widget.RelativeLayout不能强制转换为android.widget.LinearLayout
我对这个代码有一些问题。 我正在尝试使用以下格式创建动态新闻列表:Java ClassCastException:android.widget.RelativeLayout不能强制转换为android.widget.LinearLayout,java,android,android-layout,Java,Android,Android Layout,我对这个代码有一些问题。 我正在尝试使用以下格式创建动态新闻列表: 这是我的小循环代码(随机数据示例) void setListNews(Listl){ listaNewsPagina=l; 最终迭代器ite=listaNewsPagina.Iterator(); LinearLayout lin=(LinearLayout)findViewById(R.id.linear\u sezione\u news); LinearLayout lineare=(LinearLayout)findV
这是我的小循环代码(随机数据示例)
void setListNews(Listl){
listaNewsPagina=l;
最终迭代器ite=listaNewsPagina.Iterator();
LinearLayout lin=(LinearLayout)findViewById(R.id.linear\u sezione\u news);
LinearLayout lineare=(LinearLayout)findViewById(R.id.lineare);
while(ite.hasNext()){
Map Map=(Map)ite.next();
ImageView imm=(ImageView)findViewById(R.id.imEngine);
RelativeLayout rl=(RelativeLayout)findViewById(R.id.relative);
TextView titolo=(TextView)findViewById(R.id.titolo);
TextView-Sottoitolo=(TextView)findViewById(R.id.Sottoitolo);
titolo.setText(“titolooooo”);
setText(“Sottoitoloooooooooo”);
rl.addView(titolo);
rl.addView(索托蒂托洛);
lineare.addView(imm);
lineare.addView(rl);
}
林·阿德维尤(lineare);
setContentView(lin);
这是我的布局:
使用此代码启动活动时,我遇到以下问题:
05-19 09:12:45.780:E/AndroidRuntime(21729):java.lang.ClassCastException:android.widget.RelativeLayout不能强制转换为android.widget.LinearLayout
我试图用Java代码创建一个布局…但没有XD
我哪里错了?改变
LinearLayout lin = (LinearLayout)findViewById(R.id.linear_sezione_news);
到
idlinear\u sezione\u news
已分配给RelativeLayout
,因此您无法将其强制转换为LinearLayout
更改
LinearLayout lin = (LinearLayout)findViewById(R.id.linear_sezione_news);
到
id
linear\u sezione\u news
被分配到一个RelativeLayout
,因此您不能将其强制转换到LinearLayout…这个愚蠢的问题!下次我更好地阅读我的代码;)非常感谢这个错误:java.lang.NullPointerException在第行:titolo.setText(“titoloooo”);…这个愚蠢的问题!下次我更好地阅读我的代码时;)非常感谢这个错误:java.lang.NullPointerException在:titolo.setText(“titoloooo”);
LinearLayout lin = (LinearLayout)findViewById(R.id.linear_sezione_news);
RelativeLayout lin = (RelativeLayout)findViewById(R.id.linear_sezione_news);