android - custom class method access -
i have problem accessing custom class methods handles loads , saves. while same class methods worked intend. since decided reuse save/load methods couple of times needed them in separate class, when try access method program crashes, have solution explanation why? maybe problem mode_private tried changing , still crashes
package com.main.kanji_sama; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.checkbox; import com.main.customclass.sharedpreferences; public class hiragana extends activity implements onclicklistener{ checkbox cb1,cb2; button backbutton; sharedpreferences myfunction = new sharedpreferences(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_hiragana); cb1 = (checkbox) findviewbyid(r.id.hiragana_xx_check); cb1.setchecked(myfunction.loadsharedpref("cb1")); cb2 = (checkbox) findviewbyid(r.id.hiragana_kx_check); cb2.setchecked(myfunction.loadsharedpref("cb2")); backbutton = (button) findviewbyid(r.id.hiraganaconfbutton); backbutton.setonclicklistener(this); } @override public void onclick(view v){ startactivity(new intent(hiragana.this, main.class)); } public void oncheckboxclicked(view view) { boolean checked = ((checkbox) view).ischecked(); switch(view.getid()) { case r.id.hiragana_xx_check: myfunction.savesharedpref("cb1",checked); break; case r.id.hiragana_kx_check: myfunction.savesharedpref("cb2", checked); } } } **this class** public class sharedpreferences extends activity { public boolean loadsharedpref(string key){ sharedpreferences preferences = getapplicationcontext().getsharedpreferences("kanji-sama", android.content.context.mode_private); return preferences.getboolean(key, false); } public void savesharedpref(string key,boolean value){ sharedpreferences preferences = getapplicationcontext().getsharedpreferences("kanji-sama", android.content.context.mode_private); sharedpreferences.editor editor = preferences.edit(); editor.putboolean(key, value); editor.commit(); } }
you no need take class , no need of extend activity
.change code this.
public class hiragana extends activity implements onclicklistener { checkbox cb1, cb2; button backbutton; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_hiragana); cb1 = (checkbox) findviewbyid(r.id.hiragana_xx_check); cb1.setchecked(myfunction.loadsharedpref("cb1")); cb2 = (checkbox) findviewbyid(r.id.hiragana_kx_check); cb2.setchecked(myfunction.loadsharedpref("cb2")); backbutton = (button) findviewbyid(r.id.hiraganaconfbutton); backbutton.setonclicklistener(this); } @override public void onclick(view v) { startactivity(new intent(hiragana.this, main.class)); } public void oncheckboxclicked(view view) { boolean checked = ((checkbox) view).ischecked(); switch (view.getid()) { case r.id.hiragana_xx_check: savesharedpref("cb1", checked); break; case r.id.hiragana_kx_check: savesharedpref("cb2", checked); } } public boolean loadsharedpref(string key) { sharedpreferences preferences = getapplicationcontext() .getsharedpreferences("kanji-sama", android.content.context.mode_private); return preferences.getboolean(key, false); } public void savesharedpref(string key, boolean value) { sharedpreferences preferences = getapplicationcontext() .getsharedpreferences("kanji-sama", android.content.context.mode_private); sharedpreferences.editor editor = preferences.edit(); editor.putboolean(key, value); editor.commit(); } }
that need write oncheckedchange
listener checkbox
.
Comments
Post a Comment