I have a custom library which needs to upload a file periodically to my server(along with other tasks). So I have created an Intent Service in my library for it, which I invoke as follows (note, this is not part of any activity, but I have created a new thread for it and I have provided context. All log messages upto this point get printed):
Intent logIntent = new Intent(mContext,LogIntentService.class);
logIntent.setAction("abc.xyz.org.mycustomlibrary.action.LOG");
File file = new File(mContext.getCacheDir(),localCache);
logIntent.putExtra(localCache, Uri.fromFile(file));
Log.d(TAG,"Log Intent Service invoked for file: " + file.toString()
+ " Cache Dir ="+mContext.getCacheDir()+ " Extra = " +
Uri.fromFile(file));
mContext.startService(logIntent);
Manifest changes are :
<!--[START log_intentserver] -->
<service
android:name="org.mypackagename.LogIntentService"
android:exported="false"></service>
<!--[END log_intentserver] -->
And at my LogIntentService I have:
@Override
protected void onHandleIntent(Intent intent) {
Log.d("On Handle of LogIntent");
if (intent != null) {
final String action = intent.getAction();
if(ACTION_LOG.equals(action)) {
........
}
}
}
public LogIntentService() {
super(LogIntentService.class.getName());
}
On my adb logcat I find the log message for the code snippet given,as below :
Log Intent Service invoked for file: /data/data/package.name/cache/LocalCache0 Cache Dir =/data/data/package.name.demo/cache Extra = file:///data/data/package.name/cache/LocalCache0
and the error message is :
W/dalvikvm( 2046): threadid=1: thread exiting with uncaught exception (group=0x41611ba8)
E/AndroidRuntime( 2046): FATAL EXCEPTION: main
E/AndroidRuntime( 2046): Process: package.name, PID: 2046
E/AndroidRuntime( 2046): java.lang.RuntimeException: Unable to instantiate service abc.xyz.org.mycustomlibrary.LogIntentService: java.lang.NullPointerException
E/AndroidRuntime( 2046): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2543)
E/AndroidRuntime( 2046): at android.app.ActivityThread.access$1800(ActivityThread.java:135)
E/AndroidRuntime( 2046): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
E/AndroidRuntime( 2046): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 2046): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime( 2046): at android.app.ActivityThread.main(ActivityThread.java:5001)
E/AndroidRuntime( 2046): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2046): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime( 2046): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
E/AndroidRuntime( 2046): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
E/AndroidRuntime( 2046): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2046): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 2046): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
E/AndroidRuntime( 2046): at abc.xyz.org.mycustomlibrary.LogIntentService.<init>(LogIntentService.java:33)
E/AndroidRuntime( 2046): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 2046): at java.lang.Class.newInstance(Class.java:1208)
E/AndroidRuntime( 2046): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2540)
E/AndroidRuntime( 2046): ... 10 more
I have read many posts along similar lines, none of which are helping me. Am I missing something here? I'm relatively new to intents in Android, so I could be overlooking some important detail too.