Question

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 {

}
Was it helpful?

Solution

  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).

OTHER TIPS

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.

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

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
}

add this and post your full Manifest.xml

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.

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.

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.

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" />

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top