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 {
}
AndroidManifest.xml
application
android:name
(if exists)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).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With