JuiCe
JuiCe

Reputation: 4201

Application initialization

I am currently making an application which only has three classes. Two Activities, and an Application class. From what I have learned of Applications so far, the class initializes itself at the start of the program, so does that mean I do not need to initialize an Object of the class in each Activity?

My program crashes at the start every time and is returning a ClassCastException, which I'm assuming has to do with my Application class since it is the only class casting I am doing in all of my code. As a local variable I have

protected BluetoothApplication myBt;

and inside my onCreate() method I call

myBt = (BluetoothApplication)getApplication();

Upvotes: 0

Views: 1077

Answers (2)

Lalit Poptani
Lalit Poptani

Reputation: 67296

No you don't need to initialise it manually but you can use getApplicationContext() to get the instance of your Application Class for eg:-

MyApplication application = ((MyApplication)getApplicationContext());

You can also access Application class from a Non-Activity class by passing a Context to that class and then using that context for getting the instance of Application class by,

MyApplication application = ((MyApplication)context.getApplicationContext());

Upvotes: 2

Kallja
Kallja

Reputation: 5472

Is the BluetoothApplication a custom subclass of Android's default Application-class? If so, then do you tell Android in your AndroidManifest.xml to use that class instead of the default Application class?

See Android Application API for more details.

Upvotes: 1

Related Questions