0

I found at Tracking user idle time within the app in Android an answer which I tried to use, but it does not work for me.

I tried to use the above mentioned code but I get NullPointerException because sPreferences is null in getElapsedTime(). Any idea how to fix this? Hopefully somebody would be kind enough to help.

Community
  • 1
  • 1
Monica
  • 389
  • 1
  • 7
  • 19

1 Answers1

1

Most likely you are not extending the application class in your Android.manifest

http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton

Assuming you used the same class name as the example (which is MyApp), you would need the following.

<application android:name=".MyApp" android:label="@string/app_name">
 ....
</application>

sPreference is set in the OnCreate method of the custom application class. If you do not extend your own application class in your manifest then this code will never be run and sPreference will never be set.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • I had it in the manifest file, although with some mistakes. So your point was useful. I reviewed the contents of the manifest file and I found some other omissions... – Monica Nov 18 '13 at 03:21