java - How to save spinner and edit text on internal storage and pass it onto another activity -
i trying collect user input in edit text view , spinner , save values internal storage, pass on view activity crashes after click on save button, appreciate if @ codes, , tell me doing wrong.
my user details saving activity
public class passwordkeeperactivity extends activity implements adapterview.onitemselectedlistener { // initialise edittext username, password, note; button save, reset; public string savedata = environment.getexternalstoragedirectory().tostring(); string[] countrynames={"google", "yahoo", "facebook", "twitter", "instagram", "bbm", "skype", "other"}; int flags[] = {r.drawable.google, r.drawable.yahoo, r.drawable.facebook, r.drawable.twitter, r.drawable.instagram, r.drawable.bbm, r.drawable.skype, r.drawable.other}; private static final int request_external_storage = 1; private static string[] permissions_storage = { manifest.permission.read_external_storage, manifest.permission.write_external_storage }; // inflating menu public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.menu, menu); return true; } // on selection of menu @override public boolean onoptionsitemselected(menuitem item) { // handle item selection switch (item.getitemid()) { case r.id.view_passwords: intent intent = new intent(this, passwordview.class); startactivity(intent); return true; default: return super.onoptionsitemselected(item); } } public void onitemselected(adapterview<?> parent, view view, int pos, long id) { // item selected. can retrieve selected item using // parent.getitematposition(pos) } public void onnothingselected(adapterview<?> parent) { // interface callback } public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home); initialise(); //getting instance of spinner , applying onitemselectedlistener on spinner spin = (spinner) findviewbyid(r.id.planets_spinner); spin.setonitemselectedlistener(this); customadapter customadapter = new customadapter(getapplicationcontext(), flags, countrynames); spin.setadapter(customadapter); //to set site edit text focus // save data textfile save.setonclicklistener(new onclicklistener() { public void onclick(view v) { // creates hidden directory if not existing file dir = new file(getcachedir(), "/sk/"); if (!dir.exists()) { dir.mkdirs(); } // saving data part string sfilename = getcachedir() + "/sk/logp.csv"; try { filewriter writer = new filewriter(sfilename, true); string countrynames, suser, spass, sadd; countrynames = suser = username.gettext().tostring(); spass = password.gettext().tostring(); sadd = note.gettext().tostring(); if ((suser.equals("")) && (spass.equals("")) && (sadd.equals(""))) { toast.maketext(getbasecontext(), "please enter @ least 1 field", toast.length_short).show(); } else { if (suser.equals("")) suser = "null"; if (spass.equals("")) spass = "null"; if (sadd.equals("")) sadd = "null"; // encrypting passwords before saving simplecrypto mcrypt = new simplecrypto(); spass = simplecrypto.bytestohex( mcrypt.encrypt(spass) ); //spass = simplecrypto.encrypt("fugly", spass); writer.append(suser); writer.append(','); writer.append(spass); writer.append(','); writer.append(sadd); writer.append('\n'); // generate whatever data want writer.flush(); writer.close(); toast.maketext(getbasecontext(), "password saved!", toast.length_short).show(); intent intent = new intent(passwordkeeperactivity.this, passwordview.class); string[] mystrings = new string[] {"google", "yahoo", "facebook", "twitter", "instagram", "bbm", "skype", "other"}; int logo[] = new int[] {r.drawable.google, r.drawable.yahoo, r.drawable.facebook, r.drawable.twitter, r.drawable.instagram, r.drawable.bbm, r.drawable.skype, r.drawable.other}; intent.putextra("strings", mystrings); intent.putextra("logos", logo); startactivity(intent); } } catch (exception e) { toast.maketext(getbasecontext(), e.getmessage(), toast.length_short).show(); } } }); // reset reset.setonclicklistener(new onclicklistener() { public void onclick(view v) { countrynames.equals("google"); note.settext(""); username.settext(""); password.settext(""); toast.maketext(getbasecontext(), "field(s) cleared!", toast.length_short).show(); } }); } public void initialise() { username = (edittext) findviewbyid(r.id.input_name); password = (edittext) findviewbyid(r.id.input_email); note = (edittext) findviewbyid(r.id.input_password); save = (button) findviewbyid(r.id.buttonsave); reset = (button) findviewbyid(r.id.buttonreset); } /** * checks if app has permission write device storage * * if app not has permission user prompted grant permissions * * @param activity */ public static void verifystoragepermissions(activity activity) { // check if have write permission int permission = activitycompat.checkselfpermission(activity, manifest.permission.write_external_storage); if (permission != packagemanager.permission_granted) { // don't have permission prompt user activitycompat.requestpermissions( activity, permissions_storage, request_external_storage ); } } }
my log
08-15 10:02:44.353 5961-5961/com.com.dreacot.dreacot.fingerprint e/androidruntime: fatal exception: main process: com.com.dreacot.dreacot.fingerprint, pid: 5961 java.lang.nullpointerexception: attempt read null array @ com.dreacot.fortpasswordkeeper.customlistadapter.getview(customlistadapter.java:35) @ android.widget.abslistview.obtainview(abslistview.java:2929) @ android.widget.listview.measureheightofchildren(listview.java:1305) @ android.widget.listview.onmeasure(listview.java:1212) @ android.view.view.measure(view.java:20151) @ android.widget.relativelayout.measurechildhorizontal(relativelayout.java:716) @ android.widget.relativelayout.onmeasure(relativelayout.java:462) @ android.view.view.measure(view.java:20151) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6330) @ android.support.design.widget.coordinatorlayout.onmeasurechild(coordinatorlayout.java:714) @ android.support.design.widget.headerscrollingviewbehavior.onmeasurechild(headerscrollingviewbehavior.java:90) @ android.support.design.widget.appbarlayout$scrollingviewbehavior.onmeasurechild(appbarlayout.java:1391) @ android.support.design.widget.coordinatorlayout.onmeasure(coordinatorlayout.java:784) @ android.view.view.measure(view.java:20151) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6330) @ android.widget.framelayout.onmeasure(framelayout.java:194) @ android.view.view.measure(view.java:20151) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6330) @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1464) @ android.widget.linearlayout.measurevertical(linearlayout.java:747) @ android.widget.linearlayout.onmeasure(linearlayout.java:629) @ android.view.view.measure(view.java:20151) @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:6330) @ android.widget.framelayout.onmeasure(framelayout.java:194) @ com.android.internal.policy.phonewindow$decorview.onmeasure(phonewindow.java:3158) @ android.view.view.measure(view.java:20151) @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:2594) @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1549) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1841) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1437) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:7397) @ android.view.choreographer$callbackrecord.run(choreographer.java:920) @ android.view.choreographer.docallbacks(choreographer.java:695) @ android.view.choreographer.doframe(choreographer.java:631) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:906) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:158) @ android.app.activitythread.main(activitythread.java:7224) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1120)
then custom list adapter
public class customlistadapter extends arrayadapter<string> { private final activity context; private final string[] countrynames; private final int flags[]; public customlistadapter(activity context, string[] countrynames, int flags[]) { super(context, r.layout.mylist, countrynames); // todo auto-generated constructor stub this.context=context; this.countrynames=countrynames; this.flags=flags; } public view getview(int position,view view,viewgroup parent) { layoutinflater inflater=context.getlayoutinflater(); view rowview=inflater.inflate(r.layout.mylist, null,true); textview txttitle = (textview) rowview.findviewbyid(r.id.item); imageview imageview = (imageview) rowview.findviewbyid(r.id.icon); textview extratxt = (textview) rowview.findviewbyid(r.id.textview1); txttitle.settext(countrynames[position]); imageview.setimageresource(flags[position]); extratxt.settext("description "+countrynames[position]); return rowview; } }
edit
the password view
listview list; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.viewpass); intent intent = getintent(); final string[] mystrings = intent.getstringarrayextra("strings"); int logo[] = intent.getintarrayextra("logo"); customlistadapter adapter=new customlistadapter(this, mystrings, logo); list=(listview)findviewbyid(r.id.list); list.setadapter(adapter); list.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub string slecteditem= mystrings[+position]; toast.maketext(getapplicationcontext(), slecteditem, toast.length_short).show(); } }); } }
you miss spell key getting value intent.
intent.getintarrayextra("logo")
instead of
intent.getintarrayextra("logos")
to avoid you'd better using constant :
pubic static final string intent_extra_key_logos = "logos"
Comments
Post a Comment