Android.App.Application Cannot Be Cast to Android.App.Activity

android.app.Application cannot be cast to android.app.Activity

You are passing the Application Context not the Activity Context with

getApplicationContext();

Wherever you are passing it pass this or ActivityName.this instead.

Since you are trying to cast the Context you pass (Application not Activity as you thought) to an Activity with

(Activity)

you get this exception because you can't cast the Application to Activity since Application is not a sub-class of Activity.

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

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.

Application cannot be cast to android.app.Activity while checking permission

Where you are passing context to this adapter use ActivityName.this or this .
Maybe now you passed

getApplicationContext();

And you get this exception because you can not cast the Application to Activity because Application is not a sub-class of Activity.

Custom global Application class breaks with android.app.Application cannot be cast to

The error states that the type of the object returned by getApplication is android.app.Application. 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"...
</application>

android.app.Application cannot be cast to android.app.Activity in fragment

Change your line of code from :

AndroidThreeTen.init((Application) getContext());

To:

AndroidThreeTen.init((Application) getContext().getApplicationContext());

Kotlin - android.app.Application cannot be cast to android.app.Activity on an Adapter

You're most likely passing an application context into the adapter, which you then cast to an Activity. So having a crash here is the expected behavior.

It's safer to provide a custom callback from your adapter to the place where it is used. This lets you create the adapter without hard wiring its behavior to the activity. Then your callback implementation can take care of the action to perform.



Related Topics



Leave a reply



Submit