我有一个菜单类,它复制一个方法四次,唯一的区别是我传入一个不同的参数,见下面的代码:
public abstract class MenuActivity extends SherlockActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
ActionBar ab = getSherlock().getActionBar();
SherlockHelper.setupActionBar(ab, this);
}
// If the menu creation is updated, please make
// sure the same changes are made in the SavedResultsActivity.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return createMenu(this, menu);
}
public static Intent InstructionsIntent(Object aActivity){
Intent i = new Intent((Context) aActivity, InstructionsActivity.class);
i.putExtra("Flag", "firstInstructions");
return i;
}
public static boolean createMenu(SherlockActivity aActivity, Menu menu){
MenuInflater inflater = ((SherlockActivity) aActivity).getSupportMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
Intent i = new Intent((Context) aActivity, InstructionsActivity.class);
i.putExtra("Flag", "firstInstructions");
menu.findItem(R.id.hearing_test).setIntent(i);
menu.findItem(R.id.learn).setIntent(new Intent((Context) aActivity, LearnActivity.class));
menu.findItem(R.id.share).setIntent(new Intent((Context) aActivity, ShareActivity.class));
menu.findItem(R.id.locate).setIntent(new Intent((Context) aActivity, MapActivity.class));
menu.findItem(R.id.saved).setIntent(new Intent((Context) aActivity, SavedResultsActivity.class));
return true;
}
public static boolean createMenu(SherlockListActivity aActivity, Menu menu){
MenuInflater inflater = aActivity.getSupportMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
Intent i = new Intent((Context) aActivity, InstructionsActivity.class);
i.putExtra("Flag", "firstInstructions");
menu.findItem(R.id.hearing_test).setIntent(i);
menu.findItem(R.id.learn).setIntent(new Intent(aActivity, LearnActivity.class));
menu.findItem(R.id.share).setIntent(new Intent(aActivity, ShareActivity.class));
menu.findItem(R.id.locate).setIntent(new Intent(aActivity, MapActivity.class));
menu.findItem(R.id.saved).setIntent(new Intent(aActivity, SavedResultsActivity.class));
return true;
}
public static boolean createMenu(SherlockFragmentActivity aActivity, Menu menu){
MenuInflater inflater = aActivity.getSupportMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
Intent i = new Intent((Context) aActivity, InstructionsActivity.class);
i.putExtra("Flag", "firstInstructions");
menu.findItem(R.id.hearing_test).setIntent(i);
menu.findItem(R.id.learn).setIntent(new Intent(aActivity, LearnActivity.class));
menu.findItem(R.id.share).setIntent(new Intent(aActivity, ShareActivity.class));
menu.findItem(R.id.locate).setIntent(new Intent(aActivity, MapActivity.class));
menu.findItem(R.id.saved).setIntent(new Intent(aActivity, SavedResultsActivity.class));
return true;
}
public static boolean createMenu(SherlockFragment aActivity, Menu menu){
MenuInflater inflater = aActivity.getSherlockActivity().getSupportMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
Intent i = new Intent((Context) aActivity.getSherlockActivity(), InstructionsActivity.class);
i.putExtra("Flag", "firstInstructions");
menu.findItem(R.id.hearing_test).setIntent(i);
menu.findItem(R.id.learn).setIntent(new Intent(aActivity.getSherlockActivity(), LearnActivity.class));
menu.findItem(R.id.share).setIntent(new Intent(aActivity.getSherlockActivity(), ShareActivity.class));
menu.findItem(R.id.locate).setIntent(new Intent(aActivity.getSherlockActivity(), MapActivity.class));
menu.findItem(R.id.saved).setIntent(new Intent(aActivity.getSherlockActivity(), SavedResultsActivity.class));
return true;
}
}
我想改变它,所以我只有一个方法将通用对象作为参数传递但我无法弄清楚如何做到这一点!我已经尝试传入一个对象,例如InstructionsIntent
方法(这不起作用所以我不得不在每个方法中复制意图),但它每次都会崩溃,除非我像我一样将它拆分出来完成。任何人都可以指出我正确的方向,谢谢。