Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.myapp.MainActivity

Getting an error in logcat when app is launched.Below is the logcat and java code, i have checked this problem in stackover flow but unable to find the solution.

MainActivity.Java

public class MainActivity extends AppCompatActivity {
    public boolean toasted = false;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!((MainActivity) getApplicationContext()).toasted) {  (MainActivity.java:28)
            Toast.makeText(MainActivity.this, "Welcome" , Toast.LENGTH_LONG).show();
            ((MainActivity)getApplicationContext()).toasted = true;
        }
}

Logcat

Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.counter.MainActivity
        at com.example.myapp.MainActivity.onCreate(MainActivity.java:28)

Solution:

public class MainActivity extends AppCompatActivity {
    public boolean toasted = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!toasted) {  
            Toast.makeText(MainActivity.this, "Welcome" , Toast.LENGTH_LONG).show();
            toasted = true;
        } 
    }

This issue is caused by you attempting to cast an Application into an Activity.

That's simply not possible because when you're casting, there's certain rules that must be followed.

Application and Activity can both be cast to a Context because they are both children of the Context class.

However, if you remember the rules of inheritance in Java, instances of children classes can be cast to their parent class, and an instance of a parent can be cast to it's child class only if it was originally that child class.

So when you use getApplicationContext(), you're getting the context for the Application rather than your Activity. This is why the cast to MainActivity will fail because MainActivity is a child of AppCompatActivity which is a child of Activity, which is completely different from Application.

There's also no reason you should require casting to do what you want. You're currently inside the MainActivity class, so you have direct access to it's instance variables. There's no need to do additional operations to fetch a context, cast it, just to assign a new variable to a variable you already have access to.

Example App Code, App.get(this) ClassCastException � Issue #81 , Any Help Appreciated ! public class App extends Application { public static App get(Activity activity) { return (App) activity. MainActivity}: java.lang. ClassCastException: android.app.Application cannot be cast to com. pressurelabs.posture.App at ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register. By continuing to use this site, you are consenting to our use of cookies.

You try to Downcast getApplicationContext() to MainActivity.

getApplicationContext() provide you the Context of the Application and it is not a part of Activity.

If you need to save the variable in your activity just use this.toasted = true

java.lang.RuntimeException: Unable to start activity - android, java.lang.ClassCastException: android.app.Application cannot be cast to I'm creating an Android app which uses Microsoft Hawaii OCR; during the test of my app, Method) 02-08 11:38:38.861: E/AndroidRuntime(4704): Caused by: java. lang. Application; /** * Base application class for Hawaii sample applications� Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Why are u casting getApplicationContext() to MainActivity? You should get the context like this

  This.getApplicationContext();

contexte android application get de partout, mais son spectacle , Tout d'abord, assurez-vous d'enregistrer votre ApplicationContext dans le fichier manist sous �tiquette android:name . En second lieu, le probl�me semble �tre� Dismiss Join GitHub today. GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.

java.lang.ClassCastException: android.app.Application cannot be , I am developing an application and it keeps stopping: My Stack Trace AppCompatButton cannot be cast to com.rey.material.widget.Button at ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang. Button at com. example.app.MainActivity.onCreate(MainActivity.java:65) at android.app.Activity. this answer answered Sep 29 '13 at 22:01 CommonsWare 641k 100 1549 1599 tried option # 1 but now i am getting this error: Caused by: java.lang.ClassCastException: com.google.android.gms.maps.MapFragment cannot be cast to android.support.v4.app.Fragment – Maven Sep 29 '13 at 22:48 @Maven: SupportMapFragment goes with android.support.v4.app

My app keeps stopping : AndroidStudio, Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com.example.counter.MainActivity at com.example.myapp.MainActivity.

Вызвано: java.lang.ClassCastException: android.app.Application , Application cannot be cast Программирование Android Ответ. Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to com. example.artem.diplom. onCreate(MainActivity.java:43) at android.app.Activity Есть один класс, наследующий от application, например MyApp.

Comments
  • Hi, why are you trying to cast MainActivity to application instead of using this instead considering you're already on the activity class?
  • Thank you but when i rotate the screen it is showing toast message again.
  • That is the expected behavior. Because when you rotate your device, your Activity is destroyed and recreated again, therefore your toasted instance variable is also destroyed and it's reset to false. You should add the toasted value to a Bundle in onSaveInstanceState(), then fetch that in your onCreate() through toasted = savedInstanceState.getBoolean(NAME, false); Make sure you use the same NAME as when you're adding the bundle in onSaveInstanceState(). This allows your toasted to recover it's previous value prior to the rotation change, so it won't Toast again.
  • When i rotate the screen(horizontal mode) again it is showing toast message