umar
umar

Reputation: 3133

Menu Handler for the activities

Please help me out regarding the menu handler . I want to make menu handler which i can call in different activities . All things are working fine . list is fetching from the server and menu are also appearing but when i click on the any menu button "Force to close" Pops up .

Here is the 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 i want the same menu to 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") + "' was 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 is what i am getting in the logcat enter image description here

Upvotes: 0

Views: 990

Answers (1)

devunwired
devunwired

Reputation: 63293

Without any specific logcat feedback about the exception your code is throwing, my best guess is Android doesn't like you crossing multiple Context instances in the way that you are by instantiating one Activity inside of another.

A much better way to accomplish your goal is to use the MenuHandler as the base class for any Activity that you want to display the menu. In other words:

  • Leave MenuHandler alone, except for getting rid of that constructor.
  • Make all your Activity classes extend MenuHandler to bring in that functionality.

Upvotes: 1

Related Questions