Android StartActivity选项菜单

时间:2014-02-06 22:56:16

标签: android android-intent menu android-activity

我正在尝试从OptionsMenu启动一个活动,但它没有启动。为什么呢?

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    case R.id.main:
        Intent intent = null;
        intent = new Intent(MainActivity.this, Impostazioni.class);
        this.startActivity(intent);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }


}

2 个答案:

答案 0 :(得分:2)

当你这样做的时候,你不能最早回来吗?

if (mDrawerToggle.onOptionsItemSelected(item)) {
    return true;
}

我认为你永远无法获得startActivity的代码。如果不是这样,您确定菜单项的标识为R.id.main吗?我想在这个方法的顶部用一个断点来调试这个代码 - 然后逐步查看调用的内容和不调用的内容。

答案 1 :(得分:0)

// Delay is in milliseconds
static final int DRAWER_DELAY = 200;

@Override
public boolean onOptionsItemSelected(MenuItem item) {

  //open navigationDrawer
  new Handler().postDelayed(openDrawerRunnable(), DRAWER_DELAY);

    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    case R.id.main:
        Intent intent = null;
        intent = new Intent(MainActivity.this, Impostazioni.class);
        this.startActivity(intent);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }

}

打开导航抽屉的方法......

private Runnable openDrawerRunnable() {
    return new Runnable() {

        @Override
        public void run() {
            mDrawerToggle.openDrawer(Gravity.LEFT);
        }
    }
}