-1

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.

tachyonflux
  • 20,103
  • 7
  • 48
  • 67
Ramya Rao
  • 86
  • 7
  • LogIntentService.class.getName() what does this return, when called separate from the rest of the program? –  Aug 12 '15 at 16:42
  • Check if on `android:name="org.mypackagename.LogIntentService` is either the full path, or the relative to the path... add a "." before it if its a relative path (package "." name) – Bonatti Aug 12 '15 at 17:01
  • @Psychrom : It returns me org.mycustomlibrary.LogIntentService. But right after that, it takes me to activity thread and then the exception occurs. – Ramya Rao Aug 12 '15 at 17:10
  • @Bonatti : I also tried `super ("LogIntentService");` , but the exception still occurs. Should this not rule out any relative path related issues? Correct me if I'm wrong. – Ramya Rao Aug 12 '15 at 17:12
  • Why does the class reference itself in it's own constructor? –  Aug 12 '15 at 17:15
  • 1
    @RamyaRao [check here](http://stackoverflow.com/questions/7144908/how-is-an-intent-service-declared-in-the-android-manifest) what I mean is `android:name="org.mypackagename.LogIntentService"` should either be`.org.mypackagename.LogIntentService` or `abc.xyz.org.mypackagename.LogIntentService` – Bonatti Aug 12 '15 at 17:25
  • @Psychrom : Isnt it just a name for the worker thread? Ive now changed it to 'super("LogIntentService")` now, not that it solves my issue though. There is a sample snippet here http://developer.android.com/guide/components/services.html that I used as reference. – Ramya Rao Aug 12 '15 at 18:42
  • @Bonatti : Thank you for the information. But that seems to be fine in my code, and there are no relative references, so cannot be the cause of my issue. – Ramya Rao Aug 12 '15 at 18:44
  • 1
    @RamyaRao Please, check if the constructor Is not giving you a problem, you seem to call `super` that has no refference. Test by replacing the `super(LogIntentService.class.getName())`with a string "example". As far as I can tell, the code is calling the parent of itself. – Bonatti Aug 12 '15 at 20:19
  • `LogIntentService.` tells you the issue is in the init phase line 33 (either the members declaration or the constructor) trying to use the context before proper initialization has been done. Move whatever is the issue in the `onStartCommand`. – njzk2 Aug 24 '15 at 16:27

1 Answers1

0

Figured that the issue was totally unrelated to intents per se. I was trying to get application context for setting shared preferences outside of my method , right after LogIntentService class creation like this:

SharedPreferences prefs = getApplicationContext().getSharedPreferences("CacheFile", MODE_PRIVATE);

On moving the line to OnHandleIntent method, issue was resolved.

Thanks for the support and help!

Ramya Rao
  • 86
  • 7