1

I am getting a ClassCastException in android studio while running my project. Can anyone tell me what is causing this error.

ClassCastException: com.intellij.psi.impl.source.PsiPlainTextFileImpl cannot be cast to com.intellij.psi.xml.XmlFile

Thanks.

David
  • 906
  • 3
  • 11
  • 23
  • what is your code??? – JIGAR Dec 12 '15 at 11:29
  • Posting the code is not a problem for me. I dont know what is causing is exception. So i dont know which part of code to share. Can you please tell me what is causing this or what code i must share? – David Dec 12 '15 at 11:34
  • Are you running Android Studio for first time? – K Neeraj Lal Dec 12 '15 at 11:35
  • What version of `Android Studio` are you using? – K Neeraj Lal Dec 12 '15 at 11:38
  • @K Neeraj Lal . AndroidStudio 2.0 Preview – David Dec 12 '15 at 11:40
  • I think @KNeerajLal is on the right track. I didn't notice before, but that definitely looks like an internal error with Android Studio. – Mike M. Dec 12 '15 at 11:42
  • 1
    Use a stable version. I think its a bug in Android Studio. – K Neeraj Lal Dec 12 '15 at 11:42
  • Thanks @K Neeraj Lal . I will try and post the status here. – David Dec 12 '15 at 11:56
  • I tried with the latest stable version of Android studio. But got the same error – David Dec 13 '15 at 05:32
  • 1
    @David Then it seems like you've got a Resource file that your IDE thinks is plain text. Make sure they're all valid XML. – Mike M. Dec 13 '15 at 07:08
  • @Mike M. Yeah Mike. I migrated the project from eclipse to Android Studio. After that the when i try to open a xml file my Event Log shows this "ClassCastException: com.intellij.psi.impl.source.PsiPlainTextFileImpl cannot be cast to com.intellij.psi.xml.XmlFile". When i create a new project the xml is opening just fine. Is there any solution for this? – David Dec 14 '15 at 11:07
  • @David Dunno. You might just have to manually copy and paste your XML files. – Mike M. Dec 14 '15 at 11:09
  • @Mike M. Ok Mike. Will give it a try. – David Dec 14 '15 at 11:15
  • @Mike M. Sovled this atlast. This Question helped me.Android Studio ClassCastException When Attempting to Open A Layout XML File . I had a 11MB xml file in values. That was the problem. After deleting that, the problem was solved – David Dec 14 '15 at 23:58
  • @David Cool. You might consider posting that as an answer. – Mike M. Dec 15 '15 at 00:00
  • @Mike M. Posted Mike. Can you pls accept that – David Dec 15 '15 at 00:04

2 Answers2

2

Sovled this atlast. This Question helped me. Android Studio ClassCastException When Attempting to Open A Layout XML File . I had a 11MB xml file in values. That was the problem. After deleting that, the problem was solved.

And if you want to use a large XML file add the below code in idea.properties and vmoptions in the bin folder of Android Studio.

**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: https://stackoverflow.com/questions/23057988/file-size-exceeds-configured-limit-2560000-code-insightfeatures-not-availabl
Community
  • 1
  • 1
David
  • 906
  • 3
  • 11
  • 23
0

It would be better if you can share some code snippets...

Anyways, as far as ClassCastException is concerned, it means you are declaring a variable of some type and assigning it to another type you have defined in a layout xml file...

for example, in the xml you may have had:

<?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>

but while connecting the component to code:

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

This will fire a ClassCastException because you are casting a Button to an ImageView variable which is as you understand not possible!

If this doesn't solve your problem then it would be better if you post some code snippets after figuring out which code snippet causes the error!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • .Thanks for answering. Posting the code is not a problem for me. I dont know what is causing is exception. So i dont know which part of code to share. Can you please tell me what is causing this or what code i must share? – David Dec 12 '15 at 11:33
  • This is the exception that is see.No line number nothing.ClassCastException: com.intellij.psi.impl.source.PsiPlainTextFileImpl cannot be cast to com.intellij.psi.xml.XmlFile. I Cant find the page, Can you? – David Dec 12 '15 at 11:44