簡體   English   中英

無法將Android.widget.RelativeLayout強制轉換為android.widget.ImageView

[英]Android.widget.RelativeLayout cannot be cast to android.widget.ImageView

我正在嘗試獲取XML布局文件中定義的ImageView的實例:

<RelativeLayout
    android:id="@+id/visual_compass_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/output_container" >

    <ImageView
        android:id="@+id/visual_compass"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="Compass"
        android:scaleType="fitStart"
        android:src="@drawable/compass_rose" />

</RelativeLayout>

但是我總是會遇到運行時錯誤:

... java.lang.ClassCastException: android.widget.RelativeLayout cannot be
cast to android.widget.ImageView ... at net.example.MainActivity.onCreate
(MainActivity.java:57)

行“ 57”是這樣的:

compassView = (ImageView) findViewById(R.id.visual_compass);

我不知道為什么會拋出這個錯誤。 我看不到這段代碼的問題。 還有人知道嗎?

謝謝FelixD。

確保在您的代碼中, compassView僅是ImageView變量,並嘗試以

  ImageView compassView = (ImageView) findViewById(R.id.visual_compass); 

如果一切正確,請嘗試從菜單Project-> clean.清理並再次構建項目Project-> clean. Proejct-> Build

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM