ClassCastException Android 工作室

ClassCast Exception Android Studio

我在 android 工作室 运行 我的项目中遇到 ClassCastException。谁能告诉我是什么导致了这个错误。

ClassCastException: com.intellij.psi.impl.source.PsiPlainTextFileImpl 无法转换为 com.intellij.psi.xml.XmlFile

谢谢。

如果能分享一些代码片段就更好了...

无论如何,就 ClassCastException 而言,这意味着您正在声明某种类型的变量并将其分配给您在布局 xml 文件中定义的另一种类型...

例如,在 xml 你可能有:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:layout_width="wrap_content">
</Button>
</LinearLayout>

但是在将组件连接到代码时:

ImageView img1 = (ImageView)context.findViewById(R.id.btn1);

这将触发 ClassCastException,因为您正在将 Button 投射到 ImageView 变量,这在您看来是不可能的!

如果这不能解决您的问题,那么您最好 post 在找出导致错误的代码片段后再截取一些代码片段!

终于解决了这个问题。这个问题帮助了我。 Android Studio ClassCastException When Attempting to Open A Layout XML File。我有一个 11MB xml 的值文件。这就是问题所在。删除后问题解决

如果您想使用大型 XML 文件,请在 idea.properties 中添加以下代码,并在 Android Studio 的 bin 文件夹中添加 vmoptions。

**Add in idea.properties**
 #-------------------------------------------------------------    --------
# Maximum file size (kilobytes) IDE     should provide code assistance for.
# The larger file is the slower its.    editor works and higher overall system     memory requirements are
# if code assistance is enabled.     Remove this property or set to very.    large number if you need
# code assistance for any files     available regardless their size.
#---------------------------------------------------------------------
idea.max.intellisense.filesize=999999


**Add in vmoptions**


-Didea.max.intellisense.filesize=999999     # <--- new line

Reference: