onMenuItemSelected永远不会执行

时间:2010-08-27 12:14:22

标签: android

当我点击菜单项时,我在DDMS中看到以下消息:

窗口已经集中,无视焦点增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@43882778

以下是Main类中忽略onMenuClick的大部分代码。


public class Main extends TabActivity {

 public static final int ACTIVITY_CREATE = 0;

 private static final int ADD_ID = Menu.FIRST;

 private Long listId;
 private DbHelper mDbHelper;
 private Cursor mCursor;

 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Set the list id
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
         listId = extras.getLong("listId");
        }

        // Open the database
        mDbHelper = new DbHelper(this);
        mDbHelper.open();

        // Setup the tabs
        createTabs();
    }

    public void createTabs() {
     mCursor = mDbHelper.fetchAllCategories(listId);
     startManagingCursor(mCursor);

     for (int i = 0; i [less than symbol] mCursor.getCount(); i++)
     {
      createTab(
    mCursor.getLong(mCursor.getColumnIndexOrThrow("_id")),
    mCursor.getString(mCursor.getColumnIndexOrThrow("category")));
     }
    }

    public void createTab(Long categoryId, String category) {
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        intent = new Intent();
      intent.putExtra("Test", category);
      intent.setClass(this, Categories.class);
  spec = tabHost.newTabSpec(category);
   spec.setContent(intent);
   spec.setIndicator(category);
  tabHost.addTab(spec);     
    }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);
  menu.add(0, ADD_ID, 0, R.string.menu_addCategory).setIcon(R.drawable.add_grey);
  return true;
 }

 @Override
 public boolean onMenuItemSelected(int featureId, MenuItem item) {
  switch (item.getItemId()) {
  case ADD_ID:
   addCategory();
   return true;
  }

  return super.onMenuItemSelected(featureId, item);
 }

 public void addCategory() {
  Intent intent = new Intent();
  intent.setClass(this, CategoryEdit.class);
  startActivityForResult(intent, ACTIVITY_CREATE);
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
  super.onActivityResult(requestCode, resultCode, intent);

  switch (requestCode) {
  case ACTIVITY_CREATE:
   if (resultCode == RESULT_OK) {
    Bundle createExtras = intent.getExtras();
    mDbHelper.addCategory(createExtras.getString("category"));

   }
  }
 }
}

最初,我的CategoryEdit.class未列在AndroidManifest.xml文件中。我已将其添加到清单中并仍然收到相同的错误。

2 个答案:

答案 0 :(得分:2)

使用::

public boolean onOptionsItemSelected(MenuItem item) {

而不是::

 public boolean onMenuItemSelected(int featureId, MenuItem item) {

答案 1 :(得分:0)

尝试将其更改为intent.setClass(TabActivity.this, CategoryEdit.class);

如果这不起作用我将需要Logcat的更多输出,你可以一行,它真的帮助很多。