Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getApplicationContext issue

Tags:

android

I have VideoApplication class which extends Application class. I have created my other java class's object in this class so that I can pass it through activities.

public class VideoApplication extends Application {
      private Client client;
      public Client getClient(){
           return client;
      }
      public void setClient(Client client){
           this.client = client;
      }
}

I have added following line in androidManifest file: android.name=".VideoApplication".

But when I add the following line to my code(MainActivity.java), the application throws a ClassCastException exception.

VideoApplication appInstance = (VideoApplication)getApplicationContext();

Where am I going wrong? Please help.

like image 705
Jacky Chan Avatar asked Feb 23 '26 17:02

Jacky Chan


2 Answers

You might be causing a ClassCastException. Try using getApplication() not getApplicationContext():

VideoApplication appInstance = (VideoApplication) getApplication();

Since you really want an Application object not a Context object.

like image 107
Sam Avatar answered Feb 27 '26 01:02

Sam


Sure I am late to answer this but

android.name=".VideoApplication 

doesn't seem right. I think it should be

android:name=".VideoApplication 

I hope its not a typo.

like image 29
sunil berman Avatar answered Feb 27 '26 01:02

sunil berman