3

I have created 2 project 1 is Lib and 2nd in Test. Lib project needs to save some data and it should be shared preference(no other option). Lib project is a jar so doesn't have any context things in it.

As per my understanding this is related to Context. Accessing SharedPreferences through static methods

So I have created Class which extends Application inside Lib Project. This is singleton class. I declare below in Lib App mainifiest file

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:name="com.test.manager.LibApp" >
 </application>

Then I added this complete project as a lib to my actual project(testApp). When I try to run i am getting null pointer exception.

Community
  • 1
  • 1
morya
  • 835
  • 2
  • 10
  • 19

1 Answers1

3

The problem is that you're declaring the application's existence in the Lib manifest file (which is completely ignored). You need to move:

<application
   android:icon="@drawable/ic_launcher"
   android:label="@string/app_name"
   android:name="com.test.manager.LibApp" >
</application>

into your testApp manifest.

Long story short, a Context cannot originate from a JAR file, it has to come from the Application utilizing the library. This means you either have to pass your App's context to the JAR API's that need it, or as I said above, declare the application/activity defined in your JAR, in the containing application's manifest.

Here is a link to a related post: android library project and Activities

which points out the limitation of libraries that your running into here.

Community
  • 1
  • 1
bstar55
  • 3,542
  • 3
  • 20
  • 24