50

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) {
    this.setName("xxxx");
    channels = new ArrayList<String>();
    userCount = new ArrayList<Integer>();
    topics = new ArrayList<String>();

    context = ctx;

    channelLayout = (LinearLayout) ((Activity) context).findViewById(R.id.channels);
}

i get a ClassCastException

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

LOGCAT

05-08 17:53:55.102    3736-3799/g.d.allinonechat E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-5357
java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
        at g.d.xxx.IRC.<init>(IRC.java:34)
        at g.d.xxx.MainActivity$1.run(MainActivity.java:49)
        at java.lang.Thread.run(Thread.java:856)
DomeWTF
  • 2,342
  • 4
  • 33
  • 46

10 Answers10

132

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.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • 2
    This is an answer? is the same that log but with a lot of lines "ClassCastException: android.app.Application cannot be cast to android.app.Activity" A solution for me is to use "this" from an Activity – vLopez Oct 19 '17 at 13:54
  • @vLopez I don't understand your question. But yes `this` will work in an `Activity` which is why I suggested passing that since the OP was not in an `Activity` – codeMagic Oct 19 '17 at 14:28
  • 1
    I was using context = getApplicationContext() changed it to context = this. solved the problem. – DeathRs May 07 '18 at 08:41
  • 1
    Very good answer. This was a no nonsense answer. I was getting this error for a few day and when I read this answer, I corrected the association of context to the solution provided. – Val May 11 '18 at 18:32
  • 2
    Saved all of us. Thank you! – bbruno5 Sep 04 '19 at 01:51
19

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

   <application
        ...
        android: name = ".BaseApplication"
        ...> ...
Tri yulianto
  • 323
  • 2
  • 7
4

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.

Gerrard
  • 819
  • 9
  • 7
2

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

Aakash Sharma
  • 427
  • 1
  • 6
  • 15
2

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.

Basel Abuhadrous
  • 1,444
  • 1
  • 14
  • 30
0

You can also try this one.

override fun registerWith( registry: PluginRegistry) {
        GeneratedPluginRegistrant.registerWith(registry as FlutterEngine)       
    //registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin")
    }

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

Jay Mungara
  • 6,663
  • 2
  • 27
  • 49
0

I had a similar problem when checking for internet connection in my viewmodel. The error in my logcat:

java.lang.ClassCastException: android.app.Application cannot be cast to com.example.newsapplication.util.AppApplication in viewmodel

I added this line in the manifest and my application worked just fine.

  android:name="com.example.newsapplication.util.AppApplication"
Peter Gichia
  • 171
  • 2
  • 5
0

In my case, I just put add name property inside the application tag in the Manifest file

<application
    android:name=".TodoApplication"
    ...
Marawan Mamdouh
  • 584
  • 1
  • 6
  • 15
0

In my case I removed the line android:name ="<some_name>" and it worked. Looks like it was looking for a class with the name that was provided earlier whereas my Project does not have the class.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
0

In my case, I deleted the android:name=".TodoApplication" from application and added it inside the activity tag.

<activity android:name=".BaseApplication"/>

Ensure you delete from the Application tag