菜单句柄的活动 [英] 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 .
下面是MEUN处理程序类
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 .
包com.droidnova.android.howto.optionmenu;
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);
}
}
下面是我在logcat中我得到
Here is what i am getting in the logcat
推荐答案
如果没有你的code抛出异常的具体logcat的反馈,我最好的猜测是Android不喜欢你穿越多个在你通过实例有一个
活动的方式上下文
实例
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.
有一个更好的方式来完成你的目标是使用 MenuHandler
作为任何活动的基类
的要显示菜单。换句话说:
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:
- 将
MenuHandler
独自一人,但为摆脱该构造方法。 - 请您所有的
活动
类扩展MenuHandler
在该功能带来的。
- Leave
MenuHandler
alone, except for getting rid of that constructor. - Make all your
Activity
classes extendMenuHandler
to bring in that functionality.
这篇关于菜单句柄的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!