java Android.widget.RelativeLayout 不能转换为 android.widget.ImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17688505/
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
Android.widget.RelativeLayout cannot be cast to android.widget.ImageView
提问by felixd
I'm trying to get the instance of a ImageView defined in an XML layout file:
我正在尝试获取在 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>
But I always get that runtime error:
但我总是得到那个运行时错误:
... java.lang.ClassCastException: android.widget.RelativeLayout cannot be
cast to android.widget.ImageView ... at net.example.MainActivity.onCreate
(MainActivity.java:57)
Line "57" is that one:
第“57”行是那一行:
compassView = (ImageView) findViewById(R.id.visual_compass);
I can't figure out why that error ist thrown; I don't see the problem with that piece of code. Does anybody elso know?
我无法弄清楚为什么会抛出该错误;我没有看到那段代码的问题。还有人知道吗?
Thank you, Felix D.
谢谢你,菲利克斯 D。
回答by GrIsHu
Make sure in your code the compassView
is ImageView
variable only and try to access it as
确保在您的代码中compassView
只有ImageView
变量并尝试访问它
ImageView compassView = (ImageView) findViewById(R.id.visual_compass);
And if everything is correct then try to clean and Build your project again from the menu Project-> clean.
and Proejct-> Build
.
如果一切正常,请尝试从菜单Project-> clean.
和Proejct-> Build
.