Question

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)
Was it helpful?

Solution

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.

OTHER TIPS

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

   <application
        ...
        android: name = ".BaseApplication"
        ...> ...

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.

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

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.

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.

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"

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

<application
    android:name=".TodoApplication"
    ...

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top