Extension of the class in the application on the Android library project?

I have a project (in Eclipse) that I turned into an Android project library to reuse part of the code in another similar project. I think I was shot in the leg, because I get the error:

Unable to start activity ComponentInfo{com.test.scroller1/com.lib.scrolltest.ScrollTestActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to com.lib.scrolltest.resAppVars

com.lib.scrolltest is my project library that instantiates an application extension class (resAppVars). In the onCreate () method, I call:

mRav = (resAppVars) getApplicationContext ();

Thus, I can use the methods in the mRav object, which otherwise would have been a lot of duplicated code in other classes (for example, passing a request to a generic select statement that returns an ArrayList of results).

What is the problem? I seem to have come across a limitation on how I implemented the Application class.

+5
source share
2

getApplicationContext() Application ( , , onCreate() ).

- , , Application . Application :

, Application. , ​​ . singleton (, ), , Context.getApplicationContext(), .

. , , .jar .

, .;)

+4

. . .

, . android:name , .

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name="com.example.lib.MyApp">

, , (<cast>) getApplication() .

+3

All Articles