Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android classcastexception on activity startup

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 {

}
like image 980
clamp Avatar asked Oct 01 '10 11:10

clamp


4 Answers

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

like image 132
Maxim Kachurovskiy Avatar answered Oct 15 '22 14:10

Maxim Kachurovskiy


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.

like image 42
slipp3ry Avatar answered Oct 15 '22 15:10

slipp3ry


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

like image 4
Mahesh Avatar answered Oct 15 '22 14:10

Mahesh


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

        setContentView(R.layout.main);
}

add this and post your full Manifest.xml

like image 2
Dominic Avatar answered Oct 15 '22 15:10

Dominic