fel*_*ixd 2 java android runtime-error classcastexception android-layout
我正在尝试获取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>
Run Code Online (Sandbox Code Playgroud)
但我总是得到运行时错误:
... java.lang.ClassCastException: android.widget.RelativeLayout cannot be
cast to android.widget.ImageView ... at net.example.MainActivity.onCreate
(MainActivity.java:57)
Run Code Online (Sandbox Code Playgroud)
线"57"是那一个:
compassView = (ImageView) findViewById(R.id.visual_compass);
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么会抛出这个错误; 我没有看到这段代码的问题.还有谁知道吗?
谢谢Felix D.
确保您的代码compassView只是ImageView变量,并尝试访问它
ImageView compassView = (ImageView) findViewById(R.id.visual_compass);
Run Code Online (Sandbox Code Playgroud)
如果一切是正确的,那么尝试清理并从菜单中再次生成项目Project-> clean.和Proejct-> Build.
| 归档时间: |
|
| 查看次数: |
13116 次 |
| 最近记录: |