[Solved] cannot be cast to

I’m trying to change a LinearLayout from another class, but when i run this code:

public class IRC extends PircBot {

ArrayList<String> channels;
ArrayList<Integer> userCount;
ArrayList<String> topics;

LinearLayout channelLayout;
Context context;

public IRC(Context ctx) {
    channels = new ArrayList<String>();
    userCount = new ArrayList<Integer>();
    topics = new ArrayList<String>();

    context = ctx;

    channelLayout = (LinearLayout) ((Activity) context).findViewById(;

i get a ClassCastException

context is the Main activity that extends Activity passed with a getApplicationContext();


05-08 17:53:55.102    3736-3799/g.d.allinonechat E/AndroidRuntime? FATAL EXCEPTION: Thread-5357
java.lang.ClassCastException: cannot be cast to
Enquirer: DomeWTF


Solution #1:

You are passing the Application Context not the Activity Context with


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


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

Respondent: DomeWTF

Solution #2:

In my case, when I’m in an activity that extends from AppCompatActivity, it did not work(Activity) getApplicationContext (), I just putthis in its place.

Respondent: codeMagic

Solution #3:

in case your project use dagger, and then this error show up
you can add this at android manifest

        android: name = ".BaseApplication"
        ...> ...
Respondent: Gerrard

Solution #4:

You are getting this error because the parameter required is Activity and you are passing it the Application.
So, either you cast application to the Activity like: (Activity)getApplicationContext();
Or you can just type the Activity like: MyActivity.this

Respondent: Tri yulianto

Solution #5:

You can also try this one.

override fun registerWith( registry: PluginRegistry) {
        GeneratedPluginRegistrant.registerWith(registry as FlutterEngine)       

I think this one is far better solution than creating a new class.

Respondent: Aakash Sharma

Solution #6:

In my case I just put android:name=".CustomBaseClass" inside the activity tag instead of the application tag inside the manifest, hope it helps someone.

Respondent: Jay Mungara

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy