nullpointerexception - Application crash during language integration android -
hello friends i'm integrating language integration in application below code
main.java
public class activitysetting extends activity { locale mylocale; radiobutton mradiobuttonmile; radiobutton mradiobuttonkilomile; spinner mspinnerlanguage; string selecteditem=""; bathroomadapter mlanadapter; string mstringgetcurrency; string ll; string[] mstringsarraylanguage = new string[] { "english", "german", "russian", "spanish" }; public static final string my_pref = "mypreferences"; adview madview; linearlayout layout; boolean firstadreceived = true; @suppresslint("newapi") @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.settings); actionbar actionbar = getactionbar(); actionbar.setbackgrounddrawable(new colordrawable(color.parsecolor("#932691"))); actionbar.settitle(r.string.set); actionbar.sethomebuttonenabled(true); mspinnerlanguage=(spinner)findviewbyid(r.id.spn_single_mul_type); mradiobuttonmile=(radiobutton)findviewbyid(r.id.rad_ml); mradiobuttonkilomile=(radiobutton)findviewbyid(r.id.rad_km); mlanadapter=new bathroomadapter(mstringsarraylanguage); mspinnerlanguage.setadapter(mlanadapter); sharedpreferences msharedpreferences = getsharedpreferences("mypreferences", 0); mstringgetcurrency=msharedpreferences.getstring("units", ""); sharedpreferences msharedpreferences1 = getsharedpreferences("commonprefs", 0); ll=msharedpreferences1.getstring("in", ""); if (ll.equalsignorecase("english")) { mspinnerlanguage.setselection(0); } else if (ll.equalsignorecase("german")) { mspinnerlanguage.setselection(1); } else if (ll.equalsignorecase("russian")) { mspinnerlanguage.setselection(2); } else if (ll.equalsignorecase("spanish")) { mspinnerlanguage.setselection(3); } madview = new adview(activitysetting.this); madview.setadsize(adsize.smart_banner); madview.setadunitid(getresources().getstring(r.string.admob_unit_id)); //its admob key declare in string file. madview.setadlistener(new adlistener() { @override public void onadloaded() { firstadreceived = true; madview.setvisibility(view.visible); } @override public void onadfailedtoload(int error) { if (!firstadreceived) { madview.setvisibility(view.gone); } } }); layout = (linearlayout) findviewbyid(r.id.adlayout); layout.addview(madview); adrequest adrequest = new adrequest.builder().build(); madview.loadad(adrequest); if (mstringgetcurrency.equalsignorecase("k")) { mradiobuttonkilomile.setchecked(true); mradiobuttonmile.setchecked(false); } else { mradiobuttonkilomile.setchecked(false); mradiobuttonmile.setchecked(true); } mradiobuttonkilomile.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { sharedpreferences sp = getsharedpreferences(my_pref, 0); sharedpreferences.editor edit = sp.edit(); edit.putstring("units", "k"); edit.commit(); } }); mradiobuttonmile.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { sharedpreferences sp = getsharedpreferences(my_pref, 0); sharedpreferences.editor edit = sp.edit(); edit.putstring("units", "m"); edit.commit(); } }); mspinnerlanguage.setonitemselectedlistener(new onitemselectedlistener() { @override public void onitemselected(adapterview<?> parent, view view,int position, long id) { selecteditem=mstringsarraylanguage[position].tostring() ; if (selecteditem .equalsignorecase("english")) { changelang("en"); } else if (selecteditem .equalsignorecase("german")) { changelang("de"); } else if (selecteditem.equalsignorecase("russian")) { changelang("ru"); } else if (selecteditem .equalsignorecase("spanish")) { changelang("es"); } } @override public void onnothingselected(adapterview<?> parent) { } }); } public class bathroomadapter extends arrayadapter<string> { string ds[]; public bathroomadapter(string[] list) { super( activitysetting.this, r.layout.spin_layout, list); this.ds = list; } @override public view getview(int position, view convertview, viewgroup parent) { convertview= getlayoutinflater().inflate(r.layout.spin_layout, null); textview imageview = (textview)convertview.findviewbyid(android.r.id.text1); imageview.settext(ds[position]); return convertview; } } public void loadlocale() { string langpref = "language"; sharedpreferences prefs = getsharedpreferences("commonprefs", activity.mode_private); string language = prefs.getstring(langpref, "en"); selecteditem=prefs.getstring("in", ""); changelang(language); } public void changelang(string lang) { if (lang.equalsignorecase("")) return; mylocale = new locale(lang); savelocale(lang); locale.setdefault(mylocale); android.content.res.configuration config = new android.content.res.configuration(); config.locale = mylocale; getbasecontext().getresources().updateconfiguration(config, getbasecontext().getresources().getdisplaymetrics()); } public void savelocale(string lang) { string langpref = "language"; sharedpreferences prefs = getsharedpreferences("commonprefs", activity.mode_private); sharedpreferences.editor editor = prefs.edit(); editor.putstring(langpref, lang); editor.putstring("in", selecteditem); editor.commit(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.next, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: intent mintentq=new intent(activitysetting.this, mainactivity.class).setflags(intent.flag_activity_clear_top); startactivity(mintentq); return true; case r.id.cl: intent mintent22=new intent(activitysetting.this, mainactivity.class).setflags(intent.flag_activity_clear_top); startactivity(mintent22); return true; case r.id.cd: intent mintent=new intent(activitysetting.this, activityfav.class).setflags(intent.flag_activity_clear_top); startactivity(mintent); return true; case r.id.normal: intent mintent1=new intent(activitysetting.this, about.class).setflags(intent.flag_activity_clear_top); startactivity(mintent1); return true; case r.id.satelite: intent mintent2=new intent(activitysetting.this, contact.class).setflags(intent.flag_activity_clear_top); startactivity(mintent2); return true; case r.id.pin: intent mintent21=new intent(activitysetting.this, pinactivity.class).setflags(intent.flag_activity_clear_top); startactivity(mintent21); return true; case r.id.st: intent mintent2ss1=new intent(activitysetting.this, activitysetting.class).setflags(intent.flag_activity_clear_top); startactivity(mintent2ss1); return true; default: return super.onoptionsitemselected(item); } } /** called when leaving activity */ /** called when returning activity */ @override public void onresume() { super.onresume(); loadlocale(); } /** called before activity destroyed */ @override public void ondestroy() { super.ondestroy(); system.gc(); unbinddrawables(layout); } private void unbinddrawables(view view) { if (view.getbackground() != null) { view.getbackground().setcallback(null); } try { ((viewgroup) view).removeallviews(); } catch (exception e) { e.printstacktrace(); } } }
when change language give me error null pointer in line actionbar.setbackgrounddrawable(new colordrawable(color.parsecolor("#932691")));
any idea how can solve this?
Comments
Post a Comment