将以下相对布局和嵌套元素(ImageView,TextView)添加到主布局后,我的应用程序在打开时关闭,错误为java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
:
这是stacktrace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fogtechnologies.expotec.urban, PID: 2898
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fogtechnologies.expotec.urban/com.fogtechnologies.expotecshell.urban.activities.ActivityExpotecMainView}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.mai(Native Method)
Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView
at com.fogtechnologies.expotecshell.urban.activities.ActivityExpotecMainView.findViews(ActivityExpotecMainView.java:163)
at com.fogtechnologies.expotecshell.urban.activities.ActivityExpotecMainView.onCreate(ActivityExpotecMainView.java:77)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
以下是我的布局文件中的代码段:
<RelativeLayout
android:id="@+id/RelativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/tvMainDownloadEvent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:textColor="@color/white"
android:onClick="downloadEventButtonAction"
android:src="@drawable/now_performing_shape"
android:scaleX="0.8"
android:scaleY="0.8" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/tvMainDownloadEvent"
android:layout_alignTop="@id/tvMainDownloadEvent"
android:layout_alignRight="@id/tvMainDownloadEvent"
android:layout_alignBottom="@id/tvMainDownloadEvent"
android:layout_margin="1dp"
android:gravity="center"
android:textColor="@color/white"
android:textSize="16sp"
android:scaleX="0.9"
android:scaleY="0.9"
android:text="Download Event" />
</RelativeLayout>
```
以下是问题的代码行:
tvMainDownloadEvent = (TextView) findViewById(R.id.tvMainDownloadEvent);
将(TextView)
更改为(ImageView)
并不能解决此问题。
如何将ID的类型更改为ImageView?
答案 0 :(得分:4)
您的Logcat会抛出 ClassCastException
抛出以指示代码已尝试将对象强制转换为 它不是实例的子类。
问题从
开始 android:id="@+id/tvMainDownloadEvent"
是 TextView ID。请勿在 ImageView 部分设置此内容。
您应该从Java类中更改它。
答案 1 :(得分:2)
问题出在你的java代码中,而不是xml中。您最有可能在TextView
方法中使用了ImageView
和findViewById()
个ID。
答案 2 :(得分:1)
将tvMainDownloadEvent
用于textview,将ivMainDownloadEvent
用于imageview。
将布局更改为
<RelativeLayout
android:id="@+id/RelativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ivMainDownloadEvent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:textColor="@color/white"
android:onClick="downloadEventButtonAction"
android:src="@drawable/now_performing_shape"
android:scaleX="0.8"
android:scaleY="0.8" />
<TextView
android:id="@+id/tvMainDownloadEvent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/ivMainDownloadEvent"
android:layout_alignTop="@id/ivMainDownloadEvent"
android:layout_alignRight="@id/ivMainDownloadEvent"
android:layout_alignBottom="@id/ivMainDownloadEvent"
android:layout_margin="1dp"
android:gravity="center"
android:textColor="@color/white"
android:textSize="16sp"
android:scaleX="0.9"
android:scaleY="0.9"
android:text="Download Event" />
</RelativeLayout>