sharedpreferences - Getting Cast Exception Android -
i'm creating simple note app in android. using 2 activity classes named main page , second activity. i'm storing data in shared preferences in second activity , want use in first activity. i'm storing data in shared preferences (string,integer) key-pair. in main activity class, when i'm getting value shared preferences integer , compare value 0,i'm getting exception java.lang.string can't cast java.lang.integer. don't know why exception coming. android studio not giving me exception when run app, app crashes. have attach code reference.
mainactivity class:
package com.example.prateeksharma.noteballondor; import android.content.context; import android.content.intent; import android.content.sharedpreferences; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.listview; import java.util.arraylist; import java.util.list; import java.util.map; public class mainpage extends actionbaractivity { notes notes = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_page); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main_page, menu); return true; } @override protected void onresume() { super.onresume(); sharedpreferences sharedpreferences = getsharedpreferences("com.example.prateeksharma.noteballondor", context.mode_private); //map<string, integer> notesmap = (map<string, integer>)sharedpreferences.getall(); list<notes> listnotes = new arraylist<>(); map<string, ?> prefsmap = sharedpreferences.getall(); (map.entry<string, ?> entry: prefsmap.entryset()) { if (entry.getvalue() == 0){ notes = new notes(); } notes.setnotetext(entry.getkey()); listnotes.add(notes); sharedpreferences.edit().putint(entry.getkey(),2); } notesarrayadapter notesarrayadapter = new notesarrayadapter(this,r.layout.list_layout, listnotes); final listview listview = (listview) findviewbyid(r.id.listview); listview.setadapter(notesarrayadapter); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { /**/ @override public void onitemclick(adapterview<?> parent, final view view, int position, long id) { notes = (notes) parent.getitematposition(position); intent intent = new intent(mainpage.this, secondactivity.class); intent.putextra("notes",notes); startactivity(intent); } }); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement switch (item.getitemid()) { case r.id.new_link: intent intent = new intent(this, secondactivity.class); intent.putextra("notes",(android.os.parcelable)null); startactivity(intent); return true; default: return super.onoptionsitemselected(item); } } } secondactivity class:
package com.example.prateeksharma.noteballondor; import android.content.context; import android.content.intent; import android.content.sharedpreferences; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.widget.edittext; public class secondactivity extends actionbaractivity { public sharedpreferences sharedpreferences; sharedpreferences.editor edit; notes notes; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_second); sharedpreferences = getsharedpreferences("com.example.prateeksharma.noteballondor", context.mode_private); edit = sharedpreferences.edit(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_second, menu); return true; } @override protected void onresume() { super.onresume(); intent intent = getintent(); notes = (notes)intent.getparcelableextra("notes"); edittext edittext = (edittext) findviewbyid(r.id.edittext); if(notes != null){ edittext.settext(notes.getnotetext()); } else{ edittext.settext(null); } } @override protected void onstop() { super.onstop(); getintent().removeextra(notes.getnotetext()); } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); intent intent = getintent(); //noinspection simplifiableifstatement switch (item.getitemid()) { case r.id.save: edittext edittext = (edittext)findviewbyid(r.id.edittext); if (notes != null){ edit.putint(string.valueof(edittext.gettext()),1); } else{ edit.putint(string.valueof(edittext.gettext()),0); } edit.commit(); return true; case r.id.delete: if(notes != null){ edit.remove(notes.getnotetext()); edit.commit(); finish(); return true; } default: return super.onoptionsitemselected(item); } } } notes class i'm using:
package com.example.prateeksharma.noteballondor; import android.os.parcel; import android.os.parcelable; /** * created dell on 02-06-2015. */ public class notes implements parcelable { private string notetext; public string getnotetext() { return notetext; } public void setnotetext(string notetext) { this.notetext = notetext; } private notes(parcel in) { notetext = in.readstring(); } public notes(){ } @override public int describecontents() { return 0; } @override public void writetoparcel(parcel dest, int flags) { dest.writestring(notetext); } @suppresswarnings("unused") public static final parcelable.creator<notes> creator = new parcelable.creator<notes>() { @override public notes createfromparcel(parcel in) { return new notes(in); } @override public notes[] newarray(int size) { return new notes[size]; } }; } can tell me why error coming in mainactivity class @ line if (entry.getvalue() == 0)
thank you..
try replace
if ((integer)entry.getvalue() == 0){ with
if((integer.parseint( entry.getvalue()) ==0){
Comments
Post a Comment