在 Android Studio 中,当我尝试运行我的项目时,我得到一个 ClassCastException
,如下所示:
引起:java.lang.ClassCastException:android.widget.EditText 无法转换为 android.widget.RelativeLayout
当 View 对象是 100% RelativeLayout 而不是 EditText 时。 如果我注释掉上面的相关代码,问题就会出现在另一个 View 元素上。
我试过清理和重建项目,我试过编辑 XML 文件并更改 ID,我试过删除 XML 文件并重新启动 Android Studio,然后创建一个新的 XML 文件,所有这些都无济于事。
感谢您的帮助。
编辑 当我向 xml 添加新 View 时,使用我从未使用过的 id,不知何故 Activity 在它自己的(并且完全独立的)xml 文件中获得空值之前..这很奇怪,因为没有理由为什么在不同的 xml 上使用简单 View 文件应该影响甚至在它之前加载的另一个 xml..
编辑2
我检查了 View rl = activityView.findViewById(R.id.notification);
并发现它获取了 etMessage,它是同一 XML 中的另一个 View 元素,实际上是一个 EditText
输入 View 。
异常发生前的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setInitialView();
}
private void setInitialView() {
RelativeLayout frame = (RelativeLayout) findViewById(R.id.activity_frame);
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View activityView = layoutInflater.inflate(R.layout.chat, null,false);
frame.removeAllViews();
frame.addView(activityView);
chatMap.put("Public", new ArrayList<ChatMessageBox>());
// move the notification layout outside the screen
RelativeLayout rl = (RelativeLayout) findViewById(R.id.notification);
TranslateAnimation moveAnim = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE, 0,
TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, -1);
moveAnim.setDuration(0);
moveAnim.setFillAfter(true);
rl.startAnimation(moveAnim);
}
此 Activity 的父 XML,在其 onCreate 中加载了 setContentView:
<?xml version="1.0" encoding="utf-8"?>
<!-- Making the drawer layout the root view -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- MAIN CONTENT -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
<!-- The ListView to be displayed when the drawer layout is active -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
以及具有适当 View 的 XML:
<?xml version="1.0" encoding="utf-8"?>
<!-- Making the drawer layout the root view -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:id="@+id/scroller"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_above="@+id/bSendMessage"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="@drawable/chat_bg" >
<LinearLayout
android:id="@+id/chatWindowContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/bSendMessage"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/ic_send" />
<EditText
android:id="@+id/etMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/bSendMessage"
android:layout_toLeftOf="@+id/bSendMessage"
android:paddingLeft="10dp"
android:background="@drawable/text"
android:textColor="#000000"
android:imeOptions="actionNone"
android:inputType="textMultiLine"
android:maxLines="4"
android:singleLine="true"
android:layout_alignParentRight="false"
android:layout_alignWithParentIfMissing="false" />
<!-- the desired element -->
<RelativeLayout
android:id="@+id/notification"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/bg_upload"
>
<TextView
android:id="@+id/tvNotification"
android:layout_gravity="center"
android:text="New Message Recieved"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textColor="#000000" />
</RelativeLayout>
</RelativeLayout>
错误日志:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.raad/com.example.raad.activities.ChatActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.RelativeLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.RelativeLayout
at com.example.raad.activities.ChatActivity.setInitialView(ChatActivity.java:336)
at com.example.raad.activities.ChatActivity.onCreate(ChatActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5122)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.access$600(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5391)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
最佳答案
问题可能出在设备本身的应用程序缓存上。 我在另一台设备上运行该应用程序,没有任何问题,所以我清除了设备中的数据和缓存(设置 -> 应用程序)并卸载了该应用程序(只是卸载没有用),现在它可以运行了。
关于android - android studio 中 View 对象的 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37293795/