AsafK
AsafK

Reputation: 2485

cwac-wakeful library - NullPointerException in AlarmReceiver

I've implemented commonsware wakeful library and for the most it is working fine. but sometimes i get the following exception :

01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): Unable to start receiver com.commonsware.cwac.wakeful.AlarmReceiver: java.lang.NullPointerException 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): java.lang.RuntimeException: Unable to start receiver com.commonsware.cwac.wakeful.AlarmReceiver: java.lang.NullPointerException 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2287) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at android.app.ActivityThread.access$1600(ActivityThread.java:140) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1313) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at android.os.Handler.dispatchMessage(Handler.java:99) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at android.os.Looper.loop(Looper.java:137) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at android.app.ActivityThread.main(ActivityThread.java:4921) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at java.lang.reflect.Method.invokeNative(Native Method) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at java.lang.reflect.Method.invoke(Method.java:511) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at dalvik.system.NativeStart.main(Native Method) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): Caused by: java.lang.NullPointerException 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at com.commonsware.cwac.wakeful.AlarmReceiver.getListener(AlarmReceiver.java:66) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at com.commonsware.cwac.wakeful.AlarmReceiver.onReceive(AlarmReceiver.java:36) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2280) 01-11 04:24:00.018 E/CustomExceptionHandler - Exception statcktrace( 7397): ... 10 more

As you can see this originates in AlarmReceiver.getListener(). I'd appreciate if someone could shed some light on this.

I also tried a different approach which was to avoid using the listener resource xml file at all (which is my suspected root cause for this exception). I did that by creating a custom class that extends com.commonsware.cwac.wakeful.AlarmReceiver and overriden getListener(). I Also changed the reference in the manifest file. But, for some reason, now sendWakefulWork is not called at all and the scheduled service is not executed.

Help please.

Upvotes: 0

Views: 127

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006564

You are missing the com.commonsware.cwac.wakeful <meta-data> in the manifest, as line 66 of AlarmReceiver.java is the first line trying to read in that metadata. The only time that I can think of this could happen only some of the time would be on broken ROMs or on hacked versions of your app (where the metadata got stripped for some reason). If it were a coding error (e.g., you have the <meta-data> element in the wrong place), it should happen all of the time.

I did that by creating a custom class that extends com.commonsware.cwac.wakeful.AlarmReceiver and overriden getListener().

WakefulIntentService does not support that. If you do not want to use AlarmReceiver, that is fine, but do not call scheduleAlarms() or cancelAlarms(). Instead, just use sendWakefulWork(), as is described in the "Basic Usage" section of the documentation.

Upvotes: 1

Related Questions