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

后端 未结 5 1784
日久生厌
日久生厌 2020-11-28 09:33

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

public class IRC extends PircBot {

ArrayList c         


        
相关标签:
5条回答
  • 2020-11-28 10:10

    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.

    0 讨论(0)
  • 2020-11-28 10:11

    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

    0 讨论(0)
  • 2020-11-28 10:25

    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.

    0 讨论(0)
  • 2020-11-28 10:27

    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.

    0 讨论(0)
  • 2020-11-28 10:28

    For me, removing android:name=".MainActivity" from <application … did the trick.

    0 讨论(0)
提交回复
热议问题