类强制转换异常 - TextView无法转换为ImageView

时间:2013-10-02 08:42:23

标签: java android

我无法理解为什么我得到类强制转换异常,因为我只将Imageview分配给Imageview,但它声明无法将ImageView强制转换为TextView。

以下是我的xml和Activity类的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:id="@+id/review_tbl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingTop="10dp" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp" >

            <TextView
                android:id="@+id/toi_lbl"
                android:background="@color/titlebackgroundcolor"
                android:text="Times Of India :"
                android:textColor="@color/titletextcolor"
                android:textSize="15dip"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/toi_txt"
                android:background="@color/titlebackgroundcolor"
                android:textColor="@color/titletextcolor"
                android:textSize="15dip"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/txt1"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:background="@drawable/star"
                android:textDirection="firstStrong" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp" >

            <TextView
                android:id="@+id/imdb_lbl"
                android:background="@color/titlebackgroundcolor"
                android:text="Imdb.com :"
                android:textColor="@color/titletextcolor"
                android:textSize="15dip"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/imdb_txt"
                android:background="@color/titlebackgroundcolor"
                android:textColor="@color/titletextcolor"
                android:textSize="15dip"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/txt1"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:background="@drawable/star"
                android:gravity="center"
                android:text=""
                android:textAlignment="center"
                android:textColor="@color/titlebackgroundcolor"
                android:textDirection="firstStrong" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp" >

            <TextView
                android:id="@+id/in_lbl"
                android:background="@color/titlebackgroundcolor"
                android:text="In.com :"
                android:textColor="@color/titletextcolor"
                android:textSize="15dip"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/in_txt"
                android:background="@color/titlebackgroundcolor"
                android:textColor="@color/titletextcolor"
                android:textSize="15dip"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/txt1"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:background="@drawable/star"
                android:gravity="center"
                android:text=""
                android:textAlignment="center"
                android:textColor="@color/titlebackgroundcolor"
                android:textDirection="firstStrong" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </TableRow>
    </TableLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:src="@drawable/loading" />

    <TextView
        android:id="@+id/movieName_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="Ranjhana"
        android:textColor="@color/titletextcolor"
        android:textSize="15dip"
        android:textStyle="bold"

         />
    <TextView
        android:id="@+id/reldate_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/movieName_txt"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:text="Released On : 12 June 2013"
        android:textColor="@color/titletextcolor" />


    <TextView
        android:id="@+id/starring_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/reldate_txt"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:text="Cast : Sonam Kapoor,Dhanush"
        android:textColor="@color/titletextcolor" />

    <TextView
        android:id="@+id/director_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/starring_txt"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:text="Director : Koi Hai"
        android:textColor="@color/titletextcolor" />

    <TextView
        android:id="@+id/producer_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/director_txt"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:text="Producer : Koi Hai"
        android:textColor="@color/titletextcolor" />

  <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="a151e2b670cd233"
        android:layout_below="@+id/producer_txt"

        ads:loadAdOnCreate="true" />
    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/adView" >

        <TextView
            android:id="@+id/description_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textIsSelectable="true"
            android:textAlignment="gravity"
            android:layout_below="@+id/producer_txt"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:textSize="15dip"
            android:text=" Raanjhanaa is an upcoming 2013 Hindi romance film, directed by Anand L. Rai and written by Himanshu Sharma. The film is produced by Krishika Lulla under the banner Eros International. The film stars Tamil film actor Dhanush in his Bollywood debut and Sonam Kapoor in the lead roles.[2] The film is scheduled to release on June 21, 2013 along with a dubbed Tamil version called Ambikapathy"
            android:textColor="@color/titletextcolor" />
    </ScrollView>


    <ImageButton
        android:id="@+id/share_btn"
        android:layout_width="30dp"
        android:layout_height="30dp"

        android:layout_alignTop="@+id/movieName_txt"
        android:layout_toRightOf="@+id/movieName_txt"
        android:onClick="shareIt"
        android:src="@drawable/shareicon" />

</RelativeLayout>

我的活动:

 super.onCreate(savedInstanceState);
         setContentView(R.layout.film_detail);

         ImageView movieImage = (ImageView)findViewById(R.id.imageView1);

我在这条线上得到例外。

 ImageView movieImage = (ImageView)findViewById(R.id.imageView1);

谢谢, 爱玛

3 个答案:

答案 0 :(得分:1)

您的android:id="@+id/txt1"文件中有.xmlImageView&amp;使用TextView时,您应为两个视图分配不同的 ID

答案 1 :(得分:1)

 <ImageView
                android:id="@+id/txt1"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:background="@drawable/star"
                android:textDirection="firstStrong" />

您已在此ImageView中将id设为txt1

答案 2 :(得分:0)

那是因为你有相同的id(txt1

android:id="@+id/txt1"

适用于xml中的ImageViewTextView。将ImageView更改为其他内容(老实说,txt1不适合作为ImageView的ID。)

<ImageView
android:id="@+id/txt1" // my id is txt1
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/star"
android:textDirection="firstStrong" />
....
<TextView
android:id="@+id/txt1" // gosh, my id is txt1 too. :(
android:layout_width="30dp"
android:layout_height="30dp"
android:background="@drawable/star"
android:gravity="center"
android:text=""
android:textAlignment="center"
android:textColor="@color/titlebackgroundcolor"
android:textDirection="firstStrong" />