c# - Getting systemNullReference excepetion when changing menu item's title - Android -
i developing android app using c#, problem when changing menu item's title outside of button event, in oncreate
method, getting systemnullreference excepetion
able inside button click event without getting excepetion. want change item's title inside oncreate method
directly when app starts, items title should changed. have far:
private imenu menu; public override bool oncreateoptionsmenu(imenu menu) { this.menu = menu; menuinflater.inflate(resource.menu.menu, menu); return base.onprepareoptionsmenu(menu); } protected override void oncreate(bundle bundle) { base.oncreate(bundle); setcontentview(resource.layout.main); button button = findviewbyid<button>(resource.id.button1); button.click += delegate { imenuitem = menu.finditem(resource.id.about); // working on button click about.settitle("about); }; if (selected == "english") // selected string value saved in shared preference { imenuitem = menu.finditem(resource.id.about); // here getting systemnullreference excepetion about.settitle("about); } }
why having exception , how solve ? in advance..
move part of code:
if (selected == "english") // selected string value saved in shared preference { imenuitem = menu.finditem(resource.id.about); // here getting systemnullreference excepetion about.settitle("about); }
into end of oncreateoptionsmenu
before return
statement.
you should because init private imenu menu;
value in oncreateoptionsmenu
, called after oncreate()
, , using private imenu menu;
in oncreate
, while not initialized far.
Comments
Post a Comment