android classcastexception on activity startup
Asked Answered
M

9

12

i have a simple activity program in android. basically the class just extends Activity. But when i start it i get a ClassCastException in the constructor of my class. i dont even have a constructor defined, so it must be in the constructor of the superclass which is Activity.

unfortunately the debugger doesnt give any detailed information on what class it is trying to cast.

here is the stacktrace:

Thread [<1> main] (Suspended (exception RuntimeException))  
    ActivityThread$PackageInfo.makeApplication(boolean, Instrumentation) line: 649  
    ActivityThread.handleBindApplication(ActivityThread$AppBindData) line: 4232 
    ActivityThread.access$3000(ActivityThread, ActivityThread$AppBindData) line: 125    
    ActivityThread$H.handleMessage(Message) line: 2071  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4627    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 868  
    ZygoteInit.main(String[]) line: 626 
    NativeStart.main(String[]) line: not available [native method]  

and when I look into the runtimeexception I get:

detailMessage "Unable to instantiate application com.test.MyApp: java.lang.ClassCastException: com.test.MyApp" (id=830067694464)

the only code is

package com.test;
import android.app.Activity;
public class MyApp extends Activity {

}
Marrakech answered 1/10, 2010 at 11:15 Comment(7)
Could you add program code here please?Thirtytwomo
cannot make out anything from the above. post your program code please.Chelseychelsie
@clamp: Step past the exception, and allow Android to collect the stack information. View it in the LogCat pane in your DDMS perspective. That may give you more information about what is going on.Suh
there is not much code to show. its an empty class that extends activityMarrakech
edit: i did post the code, but i dont think the cause is there, since it is crashing inside the android activity's constructorMarrakech
maybe if you call the onCreate with the super.onCreate in itIrreducible
i also tried this, but no differenceMarrakech
S
24
  1. Open AndroidManifest.xml
  2. Find tag application
  3. Remove attribute android:name (if exists)
  4. Add attribute android:name="android.app.Application"

This is what I did and the problem had gone.

P.S: In Step 4 use your custom application class name if you have one (that's optional).

Signorino answered 17/10, 2010 at 12:37 Comment(8)
thanks, but that doesnt help. do you really mean to set the string "android.app.Application" there or actually the name of the application?Marrakech
edited the answer, hope it became more clear. Yes, I mean exactly "android.app.Application"Signorino
ok, suddenly i have the same problem again, but this time your fix doesnt help :(Marrakech
and this time it's a classnotfoundexceptionMarrakech
I just changed android.app.Application for my App name, and it worked!Oxendine
Can someone explain to me why this is happening? What does a ClassCastException have to do with an attribute in the manifest? Has this been reported as a bug?Mia
In my app it was android:name=".MainActivity". I changed it, didn't work, changed back and it was all fine. Eclipse or Android is truly Mercurial...Carbohydrate
It didn't work for me and if it had, I'd dislike android so much. phew-Lashaun
C
5

I had the same problem.

In my case I had a class that extended application which was referenced in the manifest file as an attribute of the application tag.

When I re-factored the class Eclipse didn't include the reference in the manifest (understandably but frustrating and confusingly) which resulted in this issue.

This also ties in with Maxim's answer but the explanation would seem to be the disparity.

When I changed the attribute name to match the file that I renamed the problem was resolved.

Chaunce answered 13/2, 2011 at 19:6 Comment(2)
I know problem is old but just posted to try and help others who may come across this in the future.Chaunce
very helpful.. Facing the same problem, could you help me to solve this issue.Auroraauroral
P
4

i made a silly mistake,

my_textView = (<b>Button</b>) findViewById(R.id.tvMyTextView);

and this caused the classcastexception..

i changed this to the obvious,

my_textView = (<b>TextView</b>) findViewById(R.id.tvMyTextView);

and it worked..

i dont know how, but all i can tell is that such silly mistakes can also cause the above problem..

Palladino answered 16/5, 2012 at 16:23 Comment(1)
Yes this is very true! I spent about three hours trying to solve all these errors when I needed ImageButton and not Button and all the errors present in this post disappeared!Culpable
I
2
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
}

add this and post your full Manifest.xml

Irreducible answered 10/11, 2010 at 20:16 Comment(0)
W
2

I had the same problem occuring occasionally, sometimes apparently triggered by re-ordering items in an XML layout.

If you allow the app to continue executing after the uncaught exception, you should find that you get a much more helpful stacktrace in "adb logcat" than you can easily get out of the debugger at the point where it stops.

This will usually point you at the location where the exception actually occurred, which is generally not the location where it stops in the debugger.

In my case, it seemed to be a mismatch between resource IDs - moving things around in the XML file seemed to confuse things such that when I did view.findViewById(R.id.something) it wasn't returning me the correct object. I was casting the object to a TextView, which should have been perfectly valid according to the contents of my XML layout, but it was crashing.

You may find that you actually have a bug where you're doing an invalid cast, which will certainly cause this exception to happen, but in my case forcing a clean and rebuild solved the problem - it regenerated the compiled layouts and resource IDs from scratch, and everything worked as expected.

Wrath answered 7/1, 2014 at 15:37 Comment(1)
Yes,I had the exact same case. Cleaned the project and it worked.Viral
S
0

If using google maps:

<uses-library android:name="com.google.android.maps" /> 

Remember this line. Can't believe I forgot it and wasted so much time.

Sparling answered 9/12, 2011 at 12:15 Comment(0)
C
0

I agree with Mahesh's answer.. This will happen due to assigning the view without casting...

This will also happen if you're casting an ImageButton View to Button View or some other View Class.

Cruz answered 22/9, 2012 at 20:35 Comment(0)
P
0

I had same problem, and I figure out, that this caused wrong usage in preferences.xml <CheckBoxPreference android:key="@+id/string" /> had to be change to <CheckBoxPreference android:key="string" />

Precis answered 13/3, 2013 at 10:37 Comment(0)
M
0

Had a similar issue with a ClassCastException telling me I couldn't cast an EditText as a TextView. I wasn't trying to. Tried to clean the project but received several 'can't delete file...' errors. Closed Eclipse, reopened it, cleaned the project. The problem went away. Clearly this won't work in every case, but it's one possibility.

Malleolus answered 31/8, 2014 at 0:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.