Java ClassCastException: android.widget.RelativeLayout 不能转换为 android.widget.LinearLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23731928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:55:15  来源:igfitidea点击:

ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout

javaandroidandroid-layout

提问by Odino

I have some problems with this code. I'm trying to create a dynamically list of news with this format:

我对这段代码有一些问题。我正在尝试使用这种格式创建动态的新闻列表:



|image| - title
|image|- subtitle


this is my little cicle code (a example with random data)

这是我的小cicle代码(一个随机数据的例子)

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);

and this is my layout:

这是我的布局:

<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>

when I launch the activity with this code I have this problem:

当我使用此代码启动活动时,我遇到了这个问题:

05-19 09:12:45.780: E/AndroidRuntime(21729): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout

05-19 09:12:45.780: E/AndroidRuntime(21729): java.lang.ClassCastException: android.widget.RelativeLayout 不能转换为 android.widget.LinearLayout

I'm trying to create a layout with only Java code...but nothing XD where I'm wrong?

我正在尝试仅使用 Java 代码创建布局……但我没有错的 XD?

采纳答案by Apoorv

Change

改变

LinearLayout lin = (LinearLayout)findViewById(R.id.linear_sezione_news);

to

RelativeLayout lin = (RelativeLayout)findViewById(R.id.linear_sezione_news);

The id linear_sezione_newsis assigned to a RelativeLayoutso you can't cast it to LinearLayout

idlinear_sezione_news被分配给 aRelativeLayout所以你不能把它投射到LinearLayout