java - Menu Handler for the activities -


please me out regarding menu handler . want make menu handler can call in different activities . things working fine . list fetching server , menu appearing when click on menu button "force close" pops .

here meun handler class

package com.droidnova.android.howto.optionmenu;  import android.app.activity; import android.content.intent; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.content.intent;  public class menuhandler extends activity{     private activity activity;      public menuhandler(activity activity) {         this.activity = activity;     }      public boolean oncreateoptionsmenu(menu menu) {         menuinflater inflater = activity.getmenuinflater();         inflater.inflate(r.menu.menu, menu);         return true;     }      public boolean onoptionsitemselected(menuitem item) {         switch (item.getitemid()) {             case r.id.settings:                      intent intent = new intent(this, showsettings.class);                   startactivity(intent);             break;             case r.id.services:                      intent intent2 = new intent(this, test.class);                 startactivity(intent2);               break;             case r.id.quit:                   finish();                    break;             default:                 break;         }         return true;     } } 

over here want same menu work .

package com.droidnova.android.howto.optionmenu;

import java.util.arraylist; import java.util.hashmap;  import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import org.w3c.dom.document; import org.w3c.dom.element; import org.w3c.dom.nodelist;  import android.app.listactivity; import android.content.intent; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.toast;  public class test extends listactivity  {     private menuhandler menuhandler;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.listplaceholder);         menuhandler = new menuhandler(this);         arraylist<hashmap<string, string>> mylist = new arraylist<hashmap<string, string>>();          jsonobject json = jsonfunctions.getjsonfromurl("http://midsweden.gofreeserve.com/fetch.php");          try{              jsonarray  earthquakes = json.getjsonarray("earthquakes");              for(int i=0;i<earthquakes.length();i++){                                         hashmap<string, string> map = new hashmap<string, string>();                     jsonobject e = earthquakes.getjsonobject(i);                  map.put("id",  string.valueof(i));                 map.put("name", "earthquake name:" + e.getstring("name"));                 map.put("password", "magnitude: " +  e.getstring("password"));                 mylist.add(map);                         }                }catch(jsonexception e)        {              log.e("log_tag", "error parsing data "+e.tostring());         }          listadapter adapter = new simpleadapter(this, mylist , r.layout.test,                          new string[] { "name", "magnitude" },                          new int[] { r.id.item_title, r.id.item_subtitle });          setlistadapter(adapter);          final listview lv = getlistview();         lv.settextfilterenabled(true);           lv.setonitemclicklistener(new onitemclicklistener() {             public void onitemclick(adapterview<?> parent, view view, int position, long id) {                               @suppresswarnings("unchecked")                 hashmap<string, string> o = (hashmap<string, string>) lv.getitematposition(position);                                    toast.maketext(test.this, "id '" + o.get("id") + "' clicked.", toast.length_short).show();               }         });     }      @override     public boolean oncreateoptionsmenu(menu menu) {         return menuhandler.oncreateoptionsmenu(menu);     }      @override     public boolean onoptionsitemselected(menuitem item) {         return menuhandler.onoptionsitemselected(item);     } } 

here getting in logcat enter image description here

without specific logcat feedback exception code throwing, best guess android doesn't crossing multiple context instances in way instantiating 1 activity inside of another.

a better way accomplish goal use menuhandler base class activity want display menu. in other words:

  • leave menuhandler alone, except getting rid of constructor.
  • make activity classes extend menuhandler bring in functionality.

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -