sandalone sandalone - 1 year ago 88
Android Question

Custom global Application class breaks with " cannot be cast to"

I have a custom global class which looks like this


public class MyApp extends Application {

public String MainAct;

public String getMainAct() {
return MainAct;

public void setMainAct(String mainAct) {
MainAct = mainAct;

I want to preserve a string in this class via another
in its

String local = "myLocalVariable";
((MyApp) getApplication()).setMainAct(local); //breaks here!!!
String name = ((MyApp) getApplication()).getMainAct();

It breaks on the marked line with error:
Caused by: java.lang.ClassCastException: cannot be cast to

I've checked the code for five times and I cannot find error anywhere. Can anyone tell me where the error is?!


Answer Source

The error states that the type of the object returned by getApplication is A possible cause for this is that you failed to define the application in the manifest. Make sure that your manifest includes something in the lines of:

<application android:name=".MyApp"...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download