java - How to clear user data in android programmatically? -
hello new in android , followed tutorial explain how clear app data in android, when ever trying clear data getting error of null object reference.
main.java
public class mainactivity extends actionbaractivity { button b; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); b = (button)findviewbyid(r.id.start); adddatainappdir(); b.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { cleardataapplication.getinstance().clearapplicationdata(getbasecontext()); } }); } private void adddatainappdir() { sharedpreferences settings = getsharedpreferences("sample", 0); sharedpreferences.editor editor = settings.edit(); editor.putboolean("key1", true); editor.putstring("key2", "some strings in prefs"); editor.commit(); try { final string filecontent = "this string in file samplefile.txt"; fileoutputstream fout = openfileoutput("samplefile.txt", mode_private); outputstreamwriter osw = new outputstreamwriter(fout); osw.write(filecontent); osw.flush(); osw.close(); } catch (ioexception e) { e.printstacktrace(); } } }
cleardataapplication.java
public class cleardataapplication extends application { private static cleardataapplication instance; @override public void oncreate() { super.oncreate(); instance = this; } public static cleardataapplication getinstance() { return instance; } public void clearapplicationdata(context mcontext) { file cache = mcontext.getcachedir(); file appdir = new file(cache.getparent()); if(appdir.exists()) { string[] children = appdir.list(); for(string s : children) { if(!s.equals("lib")) { deletedir(new file(appdir, s)); } } } } public static boolean deletedir(file dir) { if (dir != null && dir.isdirectory()) { string[] children = dir.list(); (int = 0; < children.length; i++) { boolean success = deletedir(new file(dir, children[i])); if (!success) { return false; } } } return dir.delete(); } }
logcat
10-10 16:25:55.589: e/androidruntime(6121): fatal exception: main 10-10 16:25:55.589: e/androidruntime(6121): process: com.example.fetchdata, pid: 6121 10-10 16:25:55.589: e/androidruntime(6121): java.lang.nullpointerexception: attempt invoke virtual method 'void com.example.fetchdata.cleardataapplication.clearapplicationdata(android.content.context)' on null object reference 10-10 16:25:55.589: e/androidruntime(6121): @ com.example.fetchdata.mainactivity$1.onclick(mainactivity.java:29) 10-10 16:25:55.589: e/androidruntime(6121): @ android.view.view.performclick(view.java:4756) 10-10 16:25:55.589: e/androidruntime(6121): @ android.view.view$performclick.run(view.java:19749) 10-10 16:25:55.589: e/androidruntime(6121): @ android.os.handler.handlecallback(handler.java:739) 10-10 16:25:55.589: e/androidruntime(6121): @ android.os.handler.dispatchmessage(handler.java:95) 10-10 16:25:55.589: e/androidruntime(6121): @ android.os.looper.loop(looper.java:135) 10-10 16:25:55.589: e/androidruntime(6121): @ android.app.activitythread.main(activitythread.java:5221) 10-10 16:25:55.589: e/androidruntime(6121): @ java.lang.reflect.method.invoke(native method) 10-10 16:25:55.589: e/androidruntime(6121): @ java.lang.reflect.method.invoke(method.java:372) 10-10 16:25:55.589: e/androidruntime(6121): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) 10-10 16:25:55.589: e/androidruntime(6121): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694)
the npe saying cleardataapplication.getinstance()
returning null.
cleardataapplication extends application means it's intended representation of application lifetime of running process. don't need getinstance() method runtime guarantees there 1 instance. instead, activity, call getapplication()
, cast cleardataapplicatiom.
in order above work, need make change in manifest file specify cleardataapplication should used representation of application.
<application android:name=".cleardataapplication" ...>
reference: http://developer.android.com/guide/topics/manifest/application-element.html#nm
Comments
Post a Comment