ClassCastException异常:android.widget.RelativeLayout不能转换为android.widget.LinearLayout [英] ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout
问题描述
我有一些问题,与此code。
我试图创建的新闻动态清单,格式为:
I have some problems with this code. I'm trying to create a dynamically list of news with this format:
|image| - title
|image|- subtitle
这是我的一点cicle code(用随机数据为例)
this is my little cicle code (a example with random data)
void setListNews(List<Map<String,String>>l){
listaNewsPagina = l;
final Iterator ite = listaNewsPagina.iterator();
LinearLayout lin = (LinearLayout)findViewById(R.id.linear_sezione_news);
LinearLayout lineare = (LinearLayout)findViewById(R.id.lineare);
while(ite.hasNext()){
Map<String,String> map = (Map<String, String>) ite.next();
ImageView imm = (ImageView)findViewById(R.id.immagine);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.relative);
TextView titolo = (TextView)findViewById(R.id.titolo);
TextView sottoTitolo = (TextView)findViewById(R.id.sottoTitolo);
titolo.setText("titolooooo");
sottoTitolo.setText("sottoTitoloooooooooooo");
rl.addView(titolo);
rl.addView(sottoTitolo);
lineare.addView(imm);
lineare.addView(rl);
}
lin.addView(lineare);
setContentView(lin);
这是我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linear_sezione_news"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_homepage" >
<LinearLayout
android:id="@+id/lineare"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aadd99"
>
<ImageView
android:id="@+id/immagine"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:src="@drawable/ic_launcher" />
<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:background="#abfc99">
<TextView
android:id="@+id/titolo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" />
<TextView
android:id="@+id/sottoTitolo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/titolo"
android:textSize="13dp" />
</RelativeLayout>
</LinearLayout>
<ListView
android:id="@+id/listViewNews"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ImageButtonPrecc"
android:layout_alignParentLeft="true"
android:layout_below="@+id/lineare"
android:alpha="0.8"
android:background="#aadd99"
android:clickable="true"
android:padding="10dp"
android:textAlignment="center" >
</ListView>
<ImageButton
android:id="@+id/ImageButtonPrecc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_alignParentLeft="true"
android:src="@drawable/bottone_prev"
android:text="@string/load_news" />
<ImageButton
android:id="@+id/ImageButtonSucc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="@drawable/bottone_next"
android:text="@string/load_news" />
</RelativeLayout>
当我开始练习这个code我有这样的问题:
when I launch the activity with this code I have this problem:
9月5日至19日:12:45.780:E / AndroidRuntime(21729):java.lang.ClassCastException:android.widget.RelativeLayout不能转换为android.widget.LinearLayout
05-19 09:12:45.780: E/AndroidRuntime(21729): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout
我试图创建具有唯一的Java code的布局......但没有XD
在这里我错了?
I'm trying to create a layout with only Java code...but nothing XD where I'm wrong?
推荐答案
修改
LinearLayout lin = (LinearLayout)findViewById(R.id.linear_sezione_news);
到
RelativeLayout lin = (RelativeLayout)findViewById(R.id.linear_sezione_news);
该ID linear_sezione_news
分配给 RelativeLayout的
,所以你不能将它转换为的LinearLayout
The id linear_sezione_news
is assigned to a RelativeLayout
so you can't cast it to LinearLayout
这篇关于ClassCastException异常:android.widget.RelativeLayout不能转换为android.widget.LinearLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!