hi using gcmlistener service listen incoming messages. trying access shared preference in service, of tinydb.
i following error when trying access shared preference.
java.lang.runtimeexception: unable instantiate service com.artqueen.aahaan.gcm.mygcmlistenerservice: java.lang.nullpointerexception: attempt invoke virtual method 'android.content.sharedpreferences android.content.context.getsharedpreferences(java.lang.string, int)' on null object reference @ android.app.activitythread.handlecreateservice(activitythread.java:3271) @ android.app.activitythread.access$1900(activitythread.java:182) @ android.app.activitythread$h.handlemessage(activitythread.java:1558) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:145) @ android.app.activitythread.main(activitythread.java:6141) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1399) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1194) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.content.sharedpreferences android.content.context.getsharedpreferences(java.lang.string, int)' on null object reference @ android.content.contextwrapper.getsharedpreferences(contextwrapper.java:184) @ android.preference.preferencemanager.getdefaultsharedpreferences(preferencemanager.java:369) @ com.artqueen.aahaan.helper.tinydb.<init>(tinydb.java:31) @ com.artqueen.aahaan.gcm.mygcmlistenerservice.<init>(mygcmlistenerservice.java:40) @ java.lang.reflect.constructor.newinstance(native method) @ java.lang.class.newinstance(class.java:1650) @ android.app.activitythread.handlecreateservice(activitythread.java:3268) at android.app.activitythread.access$1900(activitythread.java:182) at android.app.activitythread$h.handlemessage(activitythread.java:1558) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:145) at android.app.activitythread.main(activitythread.java:6141) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1399) at com.android.internal.os.zygoteinit.main(zygoteinit.java:1194) i using gcmlistenerservice this
public class mygcmlistenerservice extends gcmlistenerservice { private static final string tag = "mygcmlistenerservice"; tinydb tinydb = new tinydb(mygcmlistenerservice.this); @override public void onmessagereceived(string from, bundle data) { string message = data.getstring("message"); string user = data.getstring("user",""); log.e("user = "," "+user); arraylist<string> blocklist = tinydb.getliststring("blocklist"); if(blocklist.contains(user)){ //dont show notification }else { //show notification sendnotification(user); } } and reference, tinydb class looks this
public class tinydb { private sharedpreferences preferences; private string default_app_imagedata_directory; private string lastimagepath = ""; public tinydb(context appcontext) { preferences = preferencemanager.getdefaultsharedpreferences(appcontext); } /** * decodes bitmap 'path' , returns * @param path image path * @return bitmap 'path' */ public bitmap getimage(string path) { bitmap bitmapfrompath = null; try { bitmapfrompath = bitmapfactory.decodefile(path); } catch (exception e) { // todo: handle exception e.printstacktrace(); } return bitmapfrompath; } /** * returns string path of last saved image * @return string path of last saved image */ public string getsavedimagepath() { return lastimagepath; } /** * saves 'thebitmap' folder 'thefolder' name 'theimagename' * @param thefolder folder path dir want save e.g "dropbox/workimages" * @param theimagename name want assign image file e.g "meatlunch.png" * @param thebitmap image want save bitmap * @return true if image saved, false otherwise */ public boolean putimage(string thefolder, string theimagename, bitmap thebitmap) { if (thefolder == null || theimagename == null || thebitmap == null) return false; this.default_app_imagedata_directory = thefolder; string mfullpath = setupfullpath(theimagename); if (!mfullpath.equals("")) { lastimagepath = mfullpath; return savebitmap(mfullpath, thebitmap); } return false; } /** * saves 'thebitmap' 'fullpath' * @param fullpath full path of image file e.g. "images/meatlunch.png" * @param thebitmap image want save bitmap * @return true if image saved, false otherwise */ public boolean putimagewithfullpath(string fullpath, bitmap thebitmap) { return !(fullpath == null || thebitmap == null) && savebitmap(fullpath, thebitmap); } /** * creates path image name 'imagename' in default_app.. directory * @param imagename name of image * @return full path of image. if failed create directory, return empty string */ private string setupfullpath(string imagename) { file mfolder = new file(environment.getexternalstoragedirectory(), default_app_imagedata_directory); if (isexternalstoragereadable() && isexternalstoragewritable() && !mfolder.exists()) { if (!mfolder.mkdirs()) { log.e("error", "failed setup folder"); return ""; } } return mfolder.getpath() + '/' + imagename; } /** * saves bitmap png file @ path 'fullpath' * @param fullpath path of image file * @param bitmap image bitmap * @return true if saved, false otherwise */ private boolean savebitmap(string fullpath, bitmap bitmap) { if (fullpath == null || bitmap == null) return false; boolean filecreated = false; boolean bitmapcompressed = false; boolean streamclosed = false; file imagefile = new file(fullpath); if (imagefile.exists()) if (!imagefile.delete()) return false; try { filecreated = imagefile.createnewfile(); } catch (ioexception e) { e.printstacktrace(); } fileoutputstream out = null; try { out = new fileoutputstream(imagefile); bitmapcompressed = bitmap.compress(compressformat.png, 100, out); } catch (exception e) { e.printstacktrace(); bitmapcompressed = false; } { if (out != null) { try { out.flush(); out.close(); streamclosed = true; } catch (ioexception e) { e.printstacktrace(); streamclosed = false; } } } return (filecreated && bitmapcompressed && streamclosed); } // getters /** * int value sharedpreferences @ 'key'. if key not found, return 'defaultvalue' * @param key sharedpreferences key * @param defaultvalue int value returned if key not found * @return int value @ 'key' or 'defaultvalue' if key not found */ public int getint(string key, int defaultvalue) { return preferences.getint(key, defaultvalue); } /** * parsed arraylist of integers sharedpreferences @ 'key' * @param key sharedpreferences key * @return arraylist of integers */ public arraylist<integer> getlistint(string key) { string[] mylist = textutils.split(preferences.getstring(key, ""), "‚‗‚"); arraylist<string> arraytolist = new arraylist<string>(arrays.aslist(mylist)); arraylist<integer> newlist = new arraylist<integer>(); (string item : arraytolist) newlist.add(integer.parseint(item)); return newlist; } /** * long value sharedpreferences @ 'key'. if key not found, return 'defaultvalue' * @param key sharedpreferences key * @param defaultvalue long value returned if key not found * @return long value @ 'key' or 'defaultvalue' if key not found */ public long getlong(string key, long defaultvalue) { return preferences.getlong(key, defaultvalue); } /** * float value sharedpreferences @ 'key'. if key not found, return 'defaultvalue' * @param key sharedpreferences key * @param defaultvalue float value returned if key not found * @return float value @ 'key' or 'defaultvalue' if key not found */ public float getfloat(string key, float defaultvalue) { return preferences.getfloat(key, defaultvalue); } /** * double value sharedpreferences @ 'key'. if exception thrown, return 'defaultvalue' * @param key sharedpreferences key * @param defaultvalue double value returned if exception thrown * @return double value @ 'key' or 'defaultvalue' if exception thrown */ public double getdouble(string key, double defaultvalue) { string number = getstring(key); try { return double.parsedouble(number); } catch (numberformatexception e) { return defaultvalue; } } /** * parsed arraylist of double sharedpreferences @ 'key' * @param key sharedpreferences key * @return arraylist of double */ public arraylist<double> getlistdouble(string key) { string[] mylist = textutils.split(preferences.getstring(key, ""), "‚‗‚"); arraylist<string> arraytolist = new arraylist<string>(arrays.aslist(mylist)); arraylist<double> newlist = new arraylist<double>(); (string item : arraytolist) newlist.add(double.parsedouble(item)); return newlist; } /** * string value sharedpreferences @ 'key'. if key not found, return "" * @param key sharedpreferences key * @return string value @ 'key' or "" (empty string) if key not found */ public string getstring(string key) { return preferences.getstring(key, ""); } /** * parsed arraylist of string sharedpreferences @ 'key' * @param key sharedpreferences key * @return arraylist of string */ public arraylist<string> getliststring(string key) { return new arraylist<string>(arrays.aslist(textutils.split(preferences.getstring(key, ""), "‚‗‚"))); } /** * boolean value sharedpreferences @ 'key'. if key not found, return 'defaultvalue' * @param key sharedpreferences key * @param defaultvalue boolean value returned if key not found * @return boolean value @ 'key' or 'defaultvalue' if key not found */ public boolean getboolean(string key, boolean defaultvalue) { return preferences.getboolean(key, defaultvalue); } /** * parsed arraylist of boolean sharedpreferences @ 'key' * @param key sharedpreferences key * @return arraylist of boolean */ public arraylist<boolean> getlistboolean(string key) { arraylist<string> mylist = getliststring(key); arraylist<boolean> newlist = new arraylist<boolean>(); (string item : mylist) { if (item.equals("true")) { newlist.add(true); } else { newlist.add(false); } } return newlist; } // put methods /** * put int value sharedpreferences 'key' , save * @param key sharedpreferences key * @param value int value added */ public void putint(string key, int value) { preferences.edit().putint(key, value).apply(); } /** * put arraylist of integer sharedpreferences 'key' , save * @param key sharedpreferences key * @param intlist arraylist of integer added */ public void putlistint(string key, arraylist<integer> intlist) { integer[] myintlist = intlist.toarray(new integer[intlist.size()]); preferences.edit().putstring(key, textutils.join("‚‗‚", myintlist)).apply(); } /** * put long value sharedpreferences 'key' , save * @param key sharedpreferences key * @param value long value added */ public void putlong(string key, long value) { preferences.edit().putlong(key, value).apply(); } /** * put float value sharedpreferences 'key' , save * @param key sharedpreferences key * @param value float value added */ public void putfloat(string key, float value) { preferences.edit().putfloat(key, value).apply(); } /** * put double value sharedpreferences 'key' , save * @param key sharedpreferences key * @param value double value added */ public void putdouble(string key, double value) { putstring(key, string.valueof(value)); } /** * put arraylist of double sharedpreferences 'key' , save * @param key sharedpreferences key * @param doublelist arraylist of double added */ public void putlistdouble(string key, arraylist<double> doublelist) { double[] mydoublelist = doublelist.toarray(new double[doublelist.size()]); preferences.edit().putstring(key, textutils.join("‚‗‚", mydoublelist)).apply(); } /** * put string value sharedpreferences 'key' , save * @param key sharedpreferences key * @param value string value added */ public void putstring(string key, string value) { preferences.edit().putstring(key, value).apply(); } /** * put arraylist of string sharedpreferences 'key' , save * @param key sharedpreferences key * @param stringlist arraylist of string added */ public void putliststring(string key, arraylist<string> stringlist) { string[] mystringlist = stringlist.toarray(new string[stringlist.size()]); preferences.edit().putstring(key, textutils.join("‚‗‚", mystringlist)).apply(); } /** * put boolean value sharedpreferences 'key' , save * @param key sharedpreferences key * @param value boolean value added */ public void putboolean(string key, boolean value) { preferences.edit().putboolean(key, value).apply(); } /** * put arraylist of boolean sharedpreferences 'key' , save * @param key sharedpreferences key * @param boollist arraylist of boolean added */ public void putlistboolean(string key, arraylist<boolean> boollist) { arraylist<string> newlist = new arraylist<string>(); (boolean item : boollist) { if (item) { newlist.add("true"); } else { newlist.add("false"); } } putliststring(key, newlist); } /** * remove sharedpreferences item 'key' * @param key sharedpreferences key */ public void remove(string key) { preferences.edit().remove(key).apply(); } /** * delete image file @ 'path' * @param path path of image file * @return true if deleted, false otherwise */ public boolean deleteimage(string path) { return new file(path).delete(); } /** * clear sharedpreferences (remove everything) */ public void clear() { preferences.edit().clear().apply(); } /** * retrieve values sharedpreferences. not modify collection return method * @return map representing list of key/value pairs sharedpreferences */ public map<string, ?> getall() { return preferences.getall(); } /** * register sharedpreferences change listener * @param listener listener object of onsharedpreferencechangelistener */ public void registeronsharedpreferencechangelistener( sharedpreferences.onsharedpreferencechangelistener listener) { preferences.registeronsharedpreferencechangelistener(listener); } /** * unregister sharedpreferences change listener * @param listener listener object of onsharedpreferencechangelistener unregistered */ public void unregisteronsharedpreferencechangelistener( sharedpreferences.onsharedpreferencechangelistener listener) { preferences.unregisteronsharedpreferencechangelistener(listener); } /** * check if external storage writable or not * @return true if writable, false otherwise */ public static boolean isexternalstoragewritable() { return environment.media_mounted.equals(environment.getexternalstoragestate()); } /** * check if external storage readable or not * @return true if readable, false otherwise */ public static boolean isexternalstoragereadable() { string state = environment.getexternalstoragestate(); return environment.media_mounted.equals(state) || environment.media_mounted_read_only.equals(state); } } i not able figure out why not able access shared preference inside service. please me out. in advance.
used getapplicationcontext()
tinydb tinydb = new tinydb(getapplicationcontext()); in service accessing context
Comments
Post a Comment